2011-07-17 31 views
0

我需要從VIMEO加載XML飼料,爲了這個,我正在使用jquery這樣的:如何使用jQuery從不同的域加載XML feed時修復XMLHttpRequest錯誤?

$(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

的可能重複[途徑來規避同源策略(http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-起源政策) – redsquare

回答

1

請閱讀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.

因此,而不是提出請求像 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/

+0

非常感謝代碼片段的作用像一個魅力,我只是閱讀有關JSON位 –

相關問題