我有一个ajax调用将数据传递给页面,然后返回一个值.
我已经从页面检索了成功的调用,但我编写了它,以便在asp中引发错误.我如何从jquery中检索该错误?
例如:
cache: false,
url: "addInterview_Code.asp",
type: "POST",
datatype: "text",
data: strData,
success: function (html) {
alert('successful : ' + html);
$("#result").html("Successful");
},
error: function (error) {
**alert('error; ' + eval(error));**
}
Run Code Online (Sandbox Code Playgroud)
这是我不理解的错误位.在函数中我需要放置什么参数,以便我可以使用我在服务器中引发的错误消息.
pal*_*aѕн 203
Ajax error函数中的必需参数是jqXHR, exception,您可以像下面这样使用它:
$.ajax({
url: 'some_unknown_page.html',
success: function (response) {
$('#post').html(response.responseText);
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
$('#post').html(msg);
},
});
Run Code Online (Sandbox Code Playgroud)
jqXHR:
它实际上是一个看起来像这样的错误对象
您还可以在自己的浏览器控制台中查看此内容,方法是使用console.log以下error功能:
error: function (jqXHR, exception) {
console.log(jqXHR);
// Your error handling logic here..
}
Run Code Online (Sandbox Code Playgroud)
我们使用status此对象的属性来获取错误代码,就像我们获得status = 404一样,这意味着找不到请求的页面.它根本不存在.基于该状态代码,我们可以将用户重定向到登录页面或我们的业务逻辑需要的任何内容.
例外:
这是字符串变量,显示异常类型.因此,如果我们收到404错误,则exception文本只是"错误".同样,我们可能会将'超时','中止'作为其他异常文本.
取消通知:该
jqXHR.success(),jqXHR.error()和jqXHR.complete()回调弃用的jQuery 1.8.要准备的代码为他们的最终消除,使用jqXHR.done(),jqXHR.fail()和jqXHR.always()来代替.
因此,如果您使用的是jQuery 1.8或更高版本,我们需要更新成功和错误函数逻辑,如: -
// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax("some_unknown_page.html")
.done(function (response) {
// success logic here
$('#post').html(response.responseText);
})
.fail(function (jqXHR, exception) {
// Our error logic here
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
$('#post').html(msg);
})
.always(function () {
alert("complete");
});
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
cze*_*asz 88
试试这个:
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
Run Code Online (Sandbox Code Playgroud)
如果您想通知您的前端有关验证错误,请尝试返回json:
dataType: 'json',
success: function(data, textStatus, jqXHR) {
console.log(data.error);
}
Run Code Online (Sandbox Code Playgroud)
你的asp脚本应该返回:
{"error": true}
Run Code Online (Sandbox Code Playgroud)
以下是排除 asp 错误的方法。
cache: false,
url: "addInterview_Code.asp",
type: "POST",
datatype: "text",
data: strData,
success: function (html) {
alert('successful : ' + html);
$("#result").html("Successful");
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 500) {
alert('Internal error: ' + jqXHR.responseText);
} else {
alert('Unexpected error.');
}
}
Run Code Online (Sandbox Code Playgroud)
error(jqXHR, textStatus, errorThrown)
Run Code Online (Sandbox Code Playgroud)
http://api.jquery.com/jQuery.ajax/