【问题标题】:Using jsonp in jquery seems to always fail在 jquery 中使用 jsonp 似乎总是失败
【发布时间】:2012-01-18 01:02:18
【问题描述】:

无论我尝试在 jquery 的 ajax 方法中使用 dataType 'jsonp' 加载什么,我都会得到“错误”,但是对于相同的 json 内容,仅使用 'json' 可以正常工作。我需要使用 jsonp,因为我正在调用外部 API 来使用。

js方法:

$.ajax({
            url: 'test3.json',
            type: 'GET',
            dataType: 'json',
            data: {
                action: 'APICALL',
                ID: '59'
            },
            success: function(data, textStatus, xhr) {
             console.log('success');
            },
            error: function(data, textStatus, xhr) {
             console.log('error: '+textStatus);
             console.log(data);
            },
            complete: function(data, textStatus, xhr) {
             console.log('complete');
            }

        });

})

test3.json 文件:

{"symbol": "IBM", "price": "91.42"}

【问题讨论】:

  • 您可以在浏览器中访问此test3.json 吗?如果是这样,请尝试完全限定的 url。
  • 你还需要 dataType: 'jsonp' 以及回调

标签: javascript jquery ajax json


【解决方案1】:

这不是 JSONP。

您需要编写一个遵循callback 参数的JSONP endpoint

【讨论】:

  • 我想我误解了 jsonp 的工作原理。我真正想做的就是获得一个预制的 json 响应并跨域使用它。
  • 正确。 JSONP 不是魔法;您需要正确实施它。参见维基百科。
猜你喜欢
  • 2014-02-23
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 2013-11-29
  • 2020-04-06
  • 2014-09-27
相关资源
最近更新 更多