2016-01-28 97 views
0

即時通訊設法編寫代碼javascript,使我能夠登錄到網站。 當我第一次發送http(獲取請求)獲取登錄網頁的網站響應正確的我的請求給我一個cookie(登錄之前)。 現在我想發送另一個POST請求中的用戶名和密碼以及cookie以獲得login身份驗證。 問題是,即時消息發送該請求只是與網頁源代碼相同,它會引發錯誤(xhr.status爲0)。 什麼可能是錯的?javascript:使用XMLHttpRequest登錄網站

這裏是我的代碼:

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(); 
} 

回答

0

我無法與POST方法解決問題。但我發現只要請求的內容是空的,在使用POSTGET方法時就沒有區別。所以解決我的問題是簡單地用GET替換第二個函數中的POST方法,並且它完美地工作。