如何检测Ajax请求是否无法加载文件.
这是我的代码供参考:
var pro = undefined; var xmlhttp; if (window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ pro = JSON.parse(xmlhttp.responseText); } } xmlhttp.open("GET","data.json",true); xmlhttp.send();
拜托,没有jQuery.
谢谢!
解决方法
您可以检查http状态是500还是500,这意味着请求
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error中发生了错误
if (xmlhttp.status >= 500){ alert('error'); }
注意:您也可以根据需要考虑其他http状态号码