jQuery 如何显示 $.ajax 调用返回的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11432834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display error returned by $.ajax call?
提问by Ahmed Fouad
I have a code that takes forever to load, and finally when I put error handler it shows alert, but I need to know what error it returned? How can I know?
我有一个需要永远加载的代码,最后当我放置错误处理程序时它显示警报,但我需要知道它返回了什么错误?我怎么知道?
EDIT: I get requested url not found, but I am certain the url: is a valid URL on my host, what could be wrong? I can even access it directly in browser.
编辑:我没有找到请求的 url,但我确定 url: 是我主机上的有效 URL,可能有什么问题?我什至可以直接在浏览器中访问它。
// process logging in a user from sidebar
$("#login-form").submit(function(event) {
$('input:submit').attr("disabled", true);
$("p.form-result").empty();
$('p.form-submit').after('<p class="loading"><img src="<?php bloginfo('template_directory'); ?>/img/loading.gif" alt="" /></p>');
$.ajax({
url: '<?php bloginfo('template_directory'); ?>/ajax/login.php',
type: 'POST',
data: $(this).serialize(),
dataType: 'json',
success: function(data){
$('.loading').remove();
$('input:submit').attr("disabled", false);
if (data.status) {
// success
$("p.form-result").html('<span class="success">' + data.message + '</span>');
window.setTimeout(function(){location.reload()},3000);
} else {
// error
$("p.form-result").html('<span class="error">' + data.message + '</span>');
}
},
error: function(data){
alert('error');
}
});
return false;
});
回答by jbrtrnd
The error
event of the jQuery function $.ajax
receives 3 arguments
error
jQuery 函数的事件$.ajax
接收 3 个参数
error : function(jqXHR, textStatus, errorThrown){
}
This is the jQuery documentationfor this event :
这是此事件的jQuery 文档:
A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests. This is an Ajax Event.
请求失败时调用的函数。该函数接收三个参数:jqXHR(在 jQuery 1.4.x 中,XMLHttpRequest)对象、描述发生的错误类型的字符串和可选的异常对象(如果发生)。第二个参数(除了 null)的可能值是“超时”、“错误”、“中止”和“解析器错误”。当发生 HTTP 错误时,errorThrown 会接收 HTTP 状态的文本部分,例如“未找到”或“内部服务器错误”。从 jQuery 1.5 开始,错误设置可以接受函数数组。每个函数都会被依次调用。注意:跨域脚本和 JSONP 请求不会调用此处理程序。这是一个 Ajax 事件。
You'll we be able to know what error is with the parameter textStatus
您将能够知道参数有什么错误 textStatus
回答by Nikhil Saswade
Error event in ajax call get executed when ajax call has some invalid arguments in it. Following function will help you to understand error code by throwing an error and displaying error definition in alert.
当 ajax 调用中包含一些无效参数时,会执行 ajax 调用中的错误事件。以下函数将通过抛出错误并在警报中显示错误定义来帮助您理解错误代码。
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
},
回答by TWickz
Use the data parameter of your error function to alert the error and its properties. It mimics your actual error.
使用错误函数的数据参数来警告错误及其属性。它模仿您的实际错误。
error: function(data){
alert(data);
}
Possible values for the data (error) object according to this question's (http://stackoverflow.com/questions/95600/jquery-error-option-in-ajax-utility) answer
根据此问题(http://stackoverflow.com/questions/95600/jquery-error-option-in-ajax-utility)的答案,数据(错误)对象的可能值
timeout - when your specified timeout is exceeded
error - http error, like 404
notmodified - when requested resource was not modified since last request
parsererror - when an xml/json response is bad
回答by Nithin Chandran
Try adding '+' between string and variable if you concatanate. Like this:
如果连接,请尝试在字符串和变量之间添加“+”。像这样:
url: '<?php bloginfo('+template_directory+'); ?>/ajax/login.php'
回答by Roberto Godoy
Change this
改变这个
url: '<?php bloginfo('template_directory'); ?>/ajax/login.php',
By this
这样
url: '<?php bloginfo("template_directory"); ?>/ajax/login.php',
:)
:)
回答by Rehan Rajpoot
You cannot check object data by using alert statement
您不能使用警报语句检查对象数据
use console.log(data) to check the error
使用 console.log(data) 检查错误
error: function(data){
console.log(data);
}