简体   繁体   English

XMLHttpRequest:POST请求状态

[英]XMLHttpRequest: POST request status

I have such part of code: 我有这段代码:

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://someurl.com", true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    if (xhr.status == 200) {
      doSomeTask();
    }
  }
}
xhr.send('login=mylogin&password=mypassword');

How can I know if my login&password are correct? 我怎么知道我的登录名和密码是否正确? In both cases xhr.status is 200. 在两种情况下,xhr.status均为200。

Invalid Login/Password attempts are not HTTP failures. 无效的登录名/密码尝试不是HTTP故障。 Only HTTP Failures return you 4xx or 5xx return codes. 只有HTTP Failures会返回4xx或5xx返回码。 You might want to use the xhr.responseText or xhr.responseXML from the response to see what your backend is returning and base your decision according to that. 您可能要使用响应中的xhr.responseText或xhr.responseXML来查看后端返回的内容,并据此做出决定。 Please refer to http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/#dfn-responsetext for how the responses are obtained. 请参阅http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/#dfn-responsetext了解如何获取响应。

Also, there are tons of good Javascript framework that hide the complexity of making Ajax calls. 另外,还有许多优秀的Javascript框架隐藏了进行Ajax调用的复杂性。 JQuery makes the job of calling AJAX scripts extremely easy. JQuery使调用AJAX脚本的工作变得非常容易。 You might want to investigate on that instead of writing raw XHR code. 您可能需要对此进行调查,而不是编写原始XHR代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM