【问题标题】:How do I retrieve the responseText of a jquery.ajax error?如何检索 jquery.ajax 错误的 responseText?
【发布时间】:2014-03-31 01:22:41
【问题描述】:

代码如下:

$.ajax({
    url: "http://server.com/rest",
    dataType: "jsonp",
    success:function(d, t, x) {
        console.log(d);
    },
    error:function(x, t, e) {
        console.log(x.responseText);
    }
});

Firebug 显示我正在收到响应,但上面的代码只记录“未定义”。 Firebug 还显示 http 状态码是 200。谁能告诉我如何检索响应?

【问题讨论】:

  • jqXHR.responseText 给我“未定义”
  • 查看@AnthonyGarcia 发布的链接
  • 无法获取错误中的responseText,因为错误情况下没有reponseText。
  • 那么firebug如何检索响应呢? -- 只是好奇

标签: jquery


【解决方案1】:

如果您使用的是 jsonp。您的服务器应该将响应绑定到回调函数。 示例

服务器响应

myFunction(someresponsehere)

客户端

function myFunction(response)
{
  //you will get the response from server here "response"
  }

【讨论】:

  • 我知道,但在这种情况下我不控制服务器。服务器只返回没有回调的数据,因此永远不会调用回调。我无法使用非 jsonp 调用,因为我收到“请求的资源上不存在‘Access-Control-Allow-Origin’标头”错误。获取数据的唯一方法是使用 jsonp。
  • @ferdtomale 比你倒霉。
猜你喜欢
  • 2014-12-04
  • 2014-11-12
  • 2010-09-06
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 2015-07-08
相关资源
最近更新 更多