我无法使用 JQuery 成功地从 vine 中提取流行的时间线来进行 REST 调用。
当我使用此代码时:
$.ajax({
type: "GET",
crossDomain: true,
dataType: "json",
url: "https://api.vineapp.com/timelines/popular",
success: function(data){
document.getElementById("#site-wrapper").innerText=data;
},
error: function(jqXHR, textStatus, errorThrown){
var json = JSON.stringify(jqXHR, null, 4);
document.getElementById("#site-wrapper").innerText=json;
}
});
我收到此错误:
XMLHttpRequest cannot load https://api.vineapp.com/timelines/popular. No 'Access-Control-Allow-Origin' header is present on the requested resource.
经过一番研究,我发现了跨域问题,长话短说,我最终将代码更改为这样。
新代码:
$.ajax({
type: "GET",
dataType: "jsonp", //changed data type to jsonp
url: "https://api.vineapp.com/timelines/popular",
success: function(data){
poke = data;
document.getElementById("#site-wrapper").innerText=data;
},
error: function(jqXHR, textStatus, errorThrown){
poke = jqXHR;
var json = JSON.stringify(jqXHR, null, 4);
document.getElementById("#site-wrapper").innerText=json;
}
});
此更改使我能够解决跨域问题,但引入了此错误:
Uncaught SyntaxError: Unexpected token :
现在我明白为什么会出现这个错误了。这是因为 Vine 返回 JSON 数据而不是 JSONP 数据(我理解它基本上是 JSON,但包裹在函数或其他东西中)。
是否有办法解决跨域问题,同时返回 JSON 数据?我尝试过不同的调用方式,但都不起作用。任何帮助都会很棒,提前致谢。
最佳答案
Vine 发布了 oEmbed API
它指出:
Response formats: JSON, XML
不幸的是,似乎不支持 JSONP。使用服务器端代理是一个有效的替代方案(以及 Twitter,Vine 所有者,建议 this approach )。
关于javascript - XMLHttpRequest 无法加载 https ://api. vineapp.com/timelines/popular。请求的资源上不存在 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25224859/