javascript - XMLHttpRequest 无法加载 https ://api. vineapp.com/timelines/popular。请求的资源上不存在 'Access-Control-Allow-Origin' header

标签 javascript jquery ajax cross-domain vine

我无法使用 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/

相关文章:

javascript - 禁用/启用动态生成的表单字段的功能

javascript - 警报不从 iframe 冒泡

javascript - 在 Durandal View 模型中返回函数和返回对象有什么区别?

javascript - jQuery 文档准备好社交共享服务的 buggyness

javascript - 如何强制同步 $.get 调用将文件内容加载到变量中?

Ajax.BeginForm 不执行异步发布

javascript - jQuery 验证submitHandler 不适用于Ajax

Javascript:无法让变量 sum 与加法运算符一起使用

javascript - 外部 Javascript 中 JQuery 调用的范围问题?

javascript - 用 d3.js/c3.js 替换图表数据集