jQuery .ajax json 错误返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7862731/
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
.ajax json return on error
提问by azz0r
$.ajax({
type: 'POST',
url: api_url+'client/'+client.id+'.json',
data: {
_method: 'delete',
id: client.id
},
success: function(data) {
$('#delete-client').html('Success');
},
error: function(data) {
$('#delete-client').css('color', 'red');
$('#delete-client').html('Error');
}
});
On the error: function the jquery would recieve this json object with a 500 header status
在错误上:函数 jquery 会收到这个带有 500 标头状态的 json 对象
{"errors":{"code":777,"message":"Method requested does not yet exist","data":[]}}
however if I use data.errors.message it doesnt show the error there. It shows a huge object with different events in chromes developer box when I console.log the return object its using
但是,如果我使用 data.errors.message 它不会在那里显示错误。当我使用 console.log 返回对象时,它会在 chromes 开发人员框中显示一个具有不同事件的巨大对象
FIXED
固定的
var error = jQuery.parseJSON(jqXHR.responseText);
$('#delete-client').html(error.errors.message);
回答by Royi Namir
add : dataType:"json"
...............
补充:dataType:"json"
…………
回答by Eric
Try reading the $.ajax
documentation:
尝试阅读$.ajax
文档:
error(jqXHR, textStatus, errorThrown)
- FunctionA 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.
error(jqXHR, textStatus, errorThrown)
-功能请求失败时调用的函数。该函数接收三个参数:(
jqXHR
在 jQuery 1.4.x 中,XMLHttpRequest)对象、描述发生的错误类型的字符串和可选的异常对象(如果发生)。第二个参数(除了 null)的可能值是“超时”、“错误”、“中止”和“解析器错误”。当发生 HTTP 错误时,errorThrown 会接收 HTTP 状态的文本部分,例如“未找到”或“内部服务器错误”。从 jQuery 1.5 开始,错误设置可以接受函数数组。每个函数都会被依次调用。注意:跨域脚本和 JSONP 请求不会调用此处理程序。这是一个 Ajax 事件。
Check the arguments list. That's not your data. That's a jqXHR object.
检查参数列表。那不是你的数据。那是一个 jqXHR 对象。
By definition, if you've successfully retrieved data from the server, you have succeeded
, not errored
. Data is data whether it contains the string "error"
or not.
根据定义,如果您已成功从服务器检索数据,则您拥有succeeded
,而不是errored
。无论是否包含字符串,数据都是数据"error"
。
You should still be using the success
callback, even though your data is describing an error. Don't send a 500 header.
success
即使您的数据描述了错误,您仍然应该使用回调。不要发送 500 标头。
$.ajax({
type: 'POST',
url: api_url+'client/'+client.id+'.json',
data: {
_method: 'delete',
id: client.id
},
success: function(data) {
if(data.errors) {
//Server not able to process the request
}
else {
$('#delete-client').html('Success');
}
},
error: function(data) {
//AJAX request not completed
$('#delete-client').css('color', 'red');
$('#delete-client').html('Error');
}
});