从外部http url读取xml文件数据

时间:2013-08-18 20:35:58

标签: jquery xml ajax

我想使用JQuery和Ajax读取从Drupal 7生成的xml文件。

当我在url中输入http url链接:''时,Ajax函数不会检索任何数据。

当我输入我的xml文件作为本地文件(没有http url)时,Ajax功能正常工作。

Ajax代码是:

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "any http url that contains xml file",
        dataType: "xml",
        success: xmlParser
    });
});

function xmlParser(xml){

$('#load').fadeOut();

$(xml).find("movie-info").each(function () {

    $(".main").append('<div class="book"><div class="title">' + $(this).find("title").text() + '</div><div class="description">' + $(this).find("field_genre").text() + '</div><div class="date">Published ' + $(this).find("field_poster").text() + '</div></div>');
    $(".book").fadeIn(1000);

});  

1 个答案:

答案 0 :(得分:0)

对于跨域jQuery ajax调用,你只有2个可靠的选项:

1:使用代理脚本在与需要请求xml的页面相同的域上发出请求

http://wiki.asp.net/page.aspx/1430/aspnet-proxy-page--used-for-cross-domain-requests-from-ajax-and-javascript/

2:使跨域服务器支持CORS。

http://www.html5rocks.com/en/tutorials/cors/

另外还有一个选项(JSON-P)由于安全考虑而不推荐,也不适用于您的情况。

http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

相关问题