2

我一直无法使用 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 数据的同时解决跨域问题?我尝试了不同的变体来进行这个调用,但它们都不起作用。任何帮助都会很棒,在此先感谢。

4

1 回答 1

0

Vine 发布了oEmbed API

它指出:

响应格式:JSON、XML

不幸的是,似乎不支持 JSONP。使用服务器端代理是一种有效的替代方案(Twitter,Vine 所有者,建议使用这种方法)。

于 2014-12-10T15:01:53.443 回答