如何使用jQuery从其他域加载XML Feed时修复XMLHttpRequest错误?

时间:2011-07-17 10:24:45

标签: jquery cross-domain

我需要从vimeo加载xml提要,为此我使用jquery:

$(function(){

// Get vimeo feed
$.ajax({
    type: "GET",
    url: "http://vimeo.com/api/v2/<my username>/videos.xml",
    dataType: "xml",
    success: function( xml ) {
        $(xml).find( "video" ).each( function() {
            console.log( $(this).find( "title" ) );
        });
    }
});

});

但我收到此错误:XMLHttpRequest无法加载http://vimeo.com/api/v2/ / videos.xml。 Access-Control-Allow-Origin不允许原点http://localhost:8888

如果有任何不同,我正在使用MAMP。

1 个答案:

答案 0 :(得分:1)

请阅读vimeo API - User

  

制作网址

     

http://vimeo.com/api/v2/username/request.output
  用户名用户的快捷方式网址或ID,电子邮件地址不起作用   请求您想要的数据。下面列出了不同的请求类型   输出指定输出类型。我们目前提供JSON,PHP和XML格式。

所以不要像 http://vimeo.com/api/v2//videos.xml 那样发出请求,而不是像http://vimeo.com/api/v2/ / videos.json那样提出请求

现在您可以使用$.getJSON来获得这样的结果。

$(document).ready(function() {
    var url = "http://vimeo.com/api/v2/{username}/videos.json?callback=?";
    $.getJSON(url, function(data) {
        var items = [];
        $.each(data, function(key, datum) {
            items.push("<ul>");
            items.push("<li>Title: " + datum.title + "</li>");
            items.push("<li>Tags: " + datum.tags + "</li>");
            items.push("</ul>");
        });
        $("#result").html(items.join(""));
    });
});

查看演示:http://jsfiddle.net/naveen/Ssdjp/1/

相关问题