问题
ajax请求执行后返回了一个canceled(状态码),并且后台能收到数据
请求源码如下:
$.ajax({
type: "post",
datatype: "text",
url: "/dept!addDept",
data: {"name": $("#name").val(), "comment": $("#comment").val()},
success: function (data) {
alert(data)
if (data == "success") {
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
setTimeout(parent.layer.close(index), 2000) //再执行关闭
}
},
error:function (XMLHttpRequest, textStatus) {
alert("error")
// 状态码
console.log(XMLHttpRequest.status);
// 状态
console.log(XMLHttpRequest.readyState);
// 错误信息
console.log(textStatus);
}
})
状态含义
0 - (未初始化)还没有调用send()方法
1 - (载入)已调用send()方法,正在发送请求
2 - (载入完成)send()方法执行完成
3 - (交互)正在解析响应内容
4 - (完成)响应内容解析完成,可以在客户端调用了
解决办法
使用同步请求
将timeout时长设置稍长一点
$.ajax({
type: "post",
datatype: "text",
url: "/dept!addDept",
data: {"name": $("#name").val(), "comment": $("#comment").val()},
async: false, // 使用同步操作
timeout : 50000, //超时时间:50秒
success: function (data) {
alert(data)
if (data == "success") {
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
setTimeout(parent.layer.close(index), 2000) //再执行关闭
}
},
error:function (XMLHttpRequest, textStatus) {
alert("error")
// 状态码
console.log(XMLHttpRequest.status);
// 状态
console.log(XMLHttpRequest.readyState);
// 错误信息
console.log(textStatus);
}
})
参考原文:原文地址
这加速的很快了
厉害了,这博客好