【问题标题】:How to get the jsonp link from this api?如何从此 api 获取 jsonp 链接?
【发布时间】:2018-05-19 21:40:06
【问题描述】:

我一直在谷歌上搜索这个,但我很困惑。我正在尝试使用来自此 api 的数据

https://github.com/artsmia/collection/blob/master/objects/0/1.json

我正在尝试使用 JSONP,因为如果我不这样做会出现跨域错误...这就是我得到的...

function foo(data) {
    var parsedData = JSON.parse(data.responseText);
    console.log(parsedData.artist);
}

var script = document.createElement('script');
script.src = 'https://github.com/artsmia/collection/blob/master/objects/0/1.json';
document.getElementsByTagName('head')[0].appendChild(script);

foo(script);

但这不起作用。我想这是因为我没有正确链接它,但我不知道如何。

【问题讨论】:

  • 只有在 API 有 JSONP 端点时才能使用 JSONP。除非服务器为您提供 JSONP 服务,否则无法获取它

标签: javascript json api jsonp


【解决方案1】:

您正在尝试从 html 页面获取数据。单击 raw 按钮并使用该 URL。您无法从不提供 jsonp 但原始端点已启用 cors 的 api 获取 jsonp

fetch('https://raw.githubusercontent.com/artsmia/collection/master/objects/0/1.json')
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(error => console.log(error))

【讨论】:

  • 嘿,这行得通。非常感谢! .................... 编辑:虽然你知道为什么原始链接适用于你的代码,但是当我尝试使用我拥有的链接时,它只是说“拒绝执行脚本,因为它的 MIME 类型 ('application/json') 不可执行,并且启用了严格的 MIME 类型检查。"
  • 这是一个位于该 url 的 html 页面,它没有启用 CORS
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 2021-02-14
相关资源
最近更新 更多