2016-01-28 54 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方法,并且它完美地工作。