对于不正确的 Ajax 操作,我将 HTTP header 代码设置为 403 并发送以下响应:
{"code":"403","status":"Forbidden","message":"You cannot do this"}
但是,在处理我的错误时我无法访问这些数据...是否可以从 jqXHR 访问“消息”数据?
类似 jqXHR.message 的东西?
非常感谢您的帮助...
编辑:
error: function (xhr) {
$(".alert").html(xhr.responseText);
},
这会返回:
{"code":"403","status":"Forbidden","message":"You cannot do this"}
但是 xhr.responseText.message 没有返回任何内容...
编辑:此代码有效:
error: function (xhr) {
var jsonResponse = JSON.parse(xhr.responseText);
$(".alert").html(jsonResponse.message);
},
最佳答案
你应该得到 jQuery 'error' 回调... http://api.jquery.com/jQuery.ajax/
error: function(xhr, status, error) {
alert(xhr.responseText);
}
(顺便说一句..你的代码?)
关于jquery - 获取带有 AJAX 错误的服务器响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19405033/