XMLHttpRequest onreadystatechange被多次调用

问题描述 投票:19回答:1

我正在用php实现登录系统并使用ajax请求。

这是我的要求

hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

hr.onreadystatechange = function() {

    var return_data = hr.responseText;
    if(hr.readyState == 4 && hr.status == 200) {
        alert('is logged');
    }else if (hr.status == 400){
        alert('is not logged');
    }
}


hr.send(vars); // Actually execute the request

但是当我收到响应时,浏览器会启动各种警报。谁能帮忙解决此问题?

php ajax
1个回答
35
投票

是的,它应该工作……。onreadystatechange可能在一个请求中被多次调用。同时提醒这两个条件的是,只要请求正常,即为[[200。

它警告:

'未记录'

并且当它得到

readystate = 4

以及Ok signal时它会发出警报:
已登录。

然后

readyState = 4

->请求已完成,响应已准备就绪

状态= 200

->表示请求已由服务器成功处理。所以,第二警报弹出,因为至少您的请求已成功处理。有关更多信息:https://www.w3schools.com/js/js_ajax_http_response.asp这里是:

xhrobj.onreadystatechange = function() { if (xhrobj.readyState == 4 && xhrobj.status == 200) { if (xhrobj.responseText) { //put your code here document.write(xhrobj.responseText); } } };

© www.soinside.com 2019 - 2024. All rights reserved.