【问题标题】:Get server response with AJAX error?获取带有 AJAX 错误的服务器响应?
【发布时间】:2013-10-24 15:54:36
【问题描述】:

对于不正确的 Ajax 操作,我将 HTTP 标头代码设置为 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 ajax json


【解决方案1】:

你应该得到 jQuery 'error' 回调... http://api.jquery.com/jQuery.ajax/

 error: function(xhr, status, error) {
    alert(xhr.responseText);
 }

(顺便说一句..你的代码?)

【讨论】:

  • 我收到“未定义”作为警报消息
猜你喜欢
  • 2015-02-12
  • 2016-03-12
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多