【问题标题】:Displaying more error details显示更多错误详情
【发布时间】:2014-01-13 11:29:07
【问题描述】:

我正在尝试使用来自外部提供程序 api 的 json 数据。当我在浏览器中输入他们的 json url 时,它会显示我需要查看的所有 json 数据。但是,当我尝试使用 jquery 访问它时,我得到了错误,即使谷歌浏览器在开发工具部分显示了正确的 json 数据:

因此,我认为这意味着数据实际上正在被检索?无论如何,这是我正在使用的脚本:

$.ajax({
    url: "https://some_url_here_from_different_domain.json",
    type: 'GET',
    dataType: 'jsonp',
    jsonp: 'callback',
    error: function(xhr, status, error) {
        alert(error); //<- returns Error: jQuery11020135121880332008_1387982597648 was not called
        alert(xhr.responseText); //<- returns undefined
    },
    success: function(data) { 
        alert("success");
    }
});

如何获取更多错误详情,以便了解导致错误出现的原因?

【问题讨论】:

标签: javascript jquery html json jsonp


【解决方案1】:

您的 JSONP 代码很好,应该可以工作,但是无论您的代码如何,您在示例中调用的端点都会返回 JSON 而不是 JSONP。

端点:https://localbitcoins.com/sell-bitcoins-online/national-bank-transfer/.json?callback=jQuery110202691209970507771_1387997968891&_=1387997968892

区别:What are the differences between JSON and JSONP?

可能的解决方案:

  1. 联系 localbitcoins 并要求他们为该 URL 启用 JSONP
  2. 设置代理服务器以提取数据并自行返回 JSONP - 好的起点可能是:https://npmjs.org/package/json-proxy

【讨论】:

  • 他们为什么要提供一个只返回json而不返回jsonp的公共api?如果数据不能跨域使用,那么数据有什么用?
【解决方案2】:

到目前为止,我的理解是,您想使用 ajax 调用获取 json 数据。

根据此链接 - Can't get json data from jQuery ajax call

使用 dataType: 'json' 而不是 'jsonp'

var jsonData;

$.ajax({
        url: "https://some_url_here_from_different_domain.json",
        dataType: 'json',
        success: function(response) {
            jsonData = response;
        }
});

【讨论】:

  • 我想我需要指定 jsonp 因为它是跨域的。请查看我的问题中的第二个链接,看看我没有指定 jsonp 会发生什么。
猜你喜欢
  • 2017-09-11
  • 2020-04-01
  • 2014-02-05
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 2019-01-08
相关资源
最近更新 更多