加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – XMLHttpRequest onreadystatechange多次调用

发布时间:2020-12-13 14:02:31 所属栏目:PHP教程 来源:网络整理
导读:我正在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.readySta
我正在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

但是当我收到响应后,浏览器会启动各种警报.任何人都可以帮助解决这个问题吗?

是的,它的工作应该……. onreadystatechange可以在一个请求中被叫几次.
并且为了提醒这两个条件是,每当请求是可以的,它是200它提醒’它没有记录’,当它有readystate = 4以及Ok信号它警报登录.

readyState = 4 //请求完成并且响应已准备就绪

status = 200 //这意味着请求被服务器成功处理

所以,第二个警报弹出,因为你的请求成功地被提交.
更多信息:
http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp
这里是:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读