【问题标题】:Getting cross domain errors with getJSON even when specifying callback即使指定回调,getJSON 也会出现跨域错误
【发布时间】:2015-04-01 16:24:19
【问题描述】:

我正在尝试使用以下内容从 Worldcat 获取有关某些书籍的元数据:

$.getJSON( " http://xisbn.worldcat.org/webservices/xid/isbn/" + $isbn[1] + "?method=getMetadata&format=json&callback=mymethod&fl=*");

其中 $isbn[1] 是十位或十三位数字(0123456789 有效)。

我可以输入http://xisbn.worldcat.org/webservices/xid/isbn/0123456789?method=getMetadata&format=json&callback=mymethod&fl=* 并得到我需要的结果,但我不会跨域这样做。

控制台正在报告XMLHttpRequest cannot load http://xisbn.worldcat.org/webservices/xid/isbn/0123456789?method=getMetadata&format=json&callback=mymethod&fl=*. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.

我在 jsfiddle 上做这一切。想也许这可能是我在本地尝试过的问题,但没有区别。我做错了什么?

【问题讨论】:

  • 如果你得到一个 cors 错误,你没有发出 jsonp 请求。您不应该自己指定方法,而是应该使用?? 作为文档中指定的回调。
  • 好的,我后来有了,但这还不够/不起作用? mymethod = function(data){ alert(data.foo); };
  • @KevinB,如果回调变量存在则为真。但是就像你说的那样,它必须是?
  • @pr3sidentspence 请提供您尝试使用callback=?? 的代码示例。它对我有用。

标签: javascript jquery json cross-domain jsonp


【解决方案1】:

我没有正确遵循 API 文档并将成功处理程序的名称放在 URL 中,而不是将其作为数据参数发送。

我有:

 $.getJSON( "http://example.com?method=getMetadata&format=json&callback=mymethod&fl=*");

但它应该是:

$.getJSON( "http:/example.com?method=getMetadata&format=json&fl=*&callback=?", mymethod);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2014-06-22
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多