0
我尝试使用下面的代码来处理Ajax错误,但它不工作处理AJAX错误(纯JS)
function ajaxPost(url, data, success, error) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status === 200) {
if (typeof success === "function") {
success(xmlhttp.responseText);
}
}else if([404, 500 , 503, 504 ].indexOf(xmlhttp.status) > -1){
if(typeof error === "function"){
error();
}
}
}
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xmlhttp.send(JSON.stringify(data));
}
我失去了在[404, 500 , 503, 504 ]
任何其他状态代码?我不是在重新发明轮子,我已经使用原生JavaScript编写了整个DOM,并且不想仅包含用于AJAX的80KB文件。请帮助我。
上述函数将数据成功发布到服务器,但在服务器不可用时无法引发错误。请帮我解决这个问题。
'的console.log(xmlhttp.status);'? – OneOfOne