[英]javascript : Login to a website using XMLHttpRequest
我試圖用javascript
編寫代碼,使我能夠登錄到網站。 當我第一次發送http(get request)以獲取登錄網頁時,網站正確響應了我的請求,這給了我一個cookie(在登錄之前)。 現在我想在另一個POST
請求中發送用戶名和密碼以及cookie,以獲取login
身份驗證。 問題是當我發送與網頁源代碼相同的請求時,它將引發錯誤( xhr.status
為0)。 那可能有什么問題呢?
這是我的代碼:
var url = "https://sess.shirazu.ac.ir/sess/Start.aspx";
var username = "myUsername";
var password = "myPassword";
var xhr;
login: function(){
xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.onreadystatechange = (e) => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status === 200) {
var pageSource = xhr.responseText; //gets the response of the request of the login page
//here is where im hashing the password with the given key in the retrieved page-source
var parser = new DOMParser();
var doc = parser.parseFromString(pageSource, "text/xml");
var RKeyElement = String(doc.getElementById("_RKey"));
var RKey = RKeyElement.substring(RKeyElement.search("value"));
RKey = RKey.substring(RKey.search("\"")+1, RKey.lastIndexOf("\"")); //getting the 32-digit-long _RKey
DoLogin(username, password , '', RKey);
}
else {
Alert.alert("error");
}
};
xhr.open('GET', url, true);
xhr.send(null);
},
}
上面的代碼效果很好,問題在於此DoLogin()函數:
function DoLogin(iId, iPass, iCode, RKey) {
var Inc = Md5High(RKey, iPass); //this works fine (Hashing the password)
var Request = xhr;
Request.onreadystatechange = (e) => {
if (Request.readyState !== 4) {
return;
}
if (Request.status === 200) {
console.log('success', Request.responseText);
}
else {
console.log('error'); //status is 0 when this occures
}
};
Request.abort();
Request.open('post', "https://sess.shirazu.ac.ir/sess/17610358812"+"/sess/Script/AjaxEnvironment.aspx?Act=MakeMember&Id=" + iId + "&Pass=" + Inc + "&Code=" + iCode, true);
Request.send();
}
我無法通過POST
方法解決問題。 但我發現只要請求的內容為空,使用POST
或GET
方法就不會有任何區別。 所以解決我問題的就是簡單地用GET
替換第二個函數中的POST
方法,它確實可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.