开发者

How to fix XMLHttpRequest error while loading XML feed form a different domain using jQuery?

开发者 https://www.devze.com 2023-03-20 17:03 出处:网络
I need to load an xml feed from vimeo, for this I am using jquery like this: $(function(){ // Get vimeo feed

I need to load an xml feed from vimeo, for this I am using jquery like this:

$(function(){

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

});

But I get this error: XMLHttpRequest cannot load http://vimeo.com/api/v2//videos.xml. Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin.

I am using MAMP if that makes any difference.


Please read the vimeo API - User

Making the URL

http://vimeo.com/api/v2/username/request.output
username Either the shortcut URL or ID of the user, an email address will NOT work.
request The data you want. The different request types are listed below.
output Specify the output type. We currently offer JSON, PHP and XML formats.

So instead of making a request like http://vimeo.com/api/v2//videos.xml make a request like http://vimeo.com/api/v2//videos.json

Now you can use $.getJSON to get the results like this.

$(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(""));
    });
});

View Demo: http://jsfiddle.net/naveen/Ssdjp/1/

0

精彩评论

暂无评论...
验证码 换一张
取 消