jquery – 使用AJAX错误获取服务器响应?

前端之家收集整理的这篇文章主要介绍了jquery – 使用AJAX错误获取服务器响应?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对于不正确的Ajax操作,我将HTTP标头代码设置为403并发送以下响应:
{"code":"403","status":"Forbidden","message":"You cannot do this"}

但是,在处理我的错误时我无法访问此数据…是否可以从jqXHR中获取“消息”数据?

像jqXHR.message这样的东西?

非常感谢您的帮助…

EDIt:

error: function (xhr) {
            $(".alert").html(xhr.responseText);
          },

这返回:

{"code":"403","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);
 }

(btw .. ur代码?)

原文链接:https://www.f2er.com/jquery/182992.html

猜你在找的jQuery相关文章