我正在用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但是当我得到响应时,浏览器会发出各种警报。有谁能帮我解决这个问题吗?
发布于 2013-01-14 02:04:26
是的,它是工作的,因为它的should.......onreadystatechange可能在一个请求中被调用几次。要提醒这两种情况,只要请求是正常的,就是200。
它会发出警报:
‘它没有被记录’
当它获得readystate=4和Ok signal时,它会发出警报:
已登录。
然后
readyState =4 -->请求已完成,响应已就绪
status= 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);
}
}
};
https://stackoverflow.com/questions/14306187
复制相似问题