在使用ajax请求的时候 ,如果服务器返回的是500错误,或者其他非正常的http错误状态码时 会提示下面的错误

而我们需要把错误信息处理出来
<script>
$.ajax({
type:'get',
url:"/admin/dologin",
async:true,
data:data,
success:function (res) {
console.log(res);
layer.msg(res.msg,{time:1000},function () {
if(res.status == 0){
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
parent.layer.close(index); //再执行关闭
parent.location.reload();
}
});
},
error:function (e) {
//返回500错误 或者其他 http状态码错误时 需要在error 回调函数中处理了 并且返回的数据还不能直接alert,
//需要使用$.parseJSON 进行转译 res.msg 是自己组装的错误信息通用变量
var res = $.parseJSON(e.responseText);
alter(res.msg);
}
});
</script>
发布时间 : 2023-02-27,阅读量:2423