Get remote xml file by AJAX and parse it with jquery

99封情书 提交于 2019-12-11 04:56:00

问题


I'm developing a HTML5 application for Blackberry OS 5+.

I'm using jQuery to download and XML file and show it using this function:

$(document).ready(function()
{
    $.ajax({
        type: "GET",
        url: "http://xxx.com/yyy/mTop",
        dataType: "xml",
        success: function(xml) {
            $(xml).find('item').each(function(){
                var tipo = $(this).find('tipo').text();
                var porcentaje = $(this).find('porcentaje').text();
                $('<div class="items"></div>').html('<p>' + tipo + ' - ' + porcentaje + '</p>').appendTo('#page-wrap');
            });
        }
    });
});

But I'm getting this error:

XMLHttpRequest cannot load http://xxx.com/yyy/mTop. Origin file:// is not allowed by Access-Control-Allow-Origin.

How can I parse a remote XML file?

Maybe I need to convert XML retrieved to a DOM object for use with jQuery.


回答1:


And the reason why you dont have a file location in your link ( url: "http ://xxx.com/yyy/mTop" ) is becouse the site "produces" an xml the moment you visit the folder, slowing down the website each time you reach it..

What you must do is:

go to http ://xxx.com/yyy/mTop on your browser right click - view source code - copy to notepad - save as .xml

upload file to another folder

then change your code url to this url: "http ://xxx.com/yyy/mTop/yourdailyXMLcopy. xml

and keep updating the file daily..else you will kill the server querying each time any user uses your thing for a huge job...




回答2:


That's because of Same Origin Policy:

The policy permits scripts running on pages originating from the same site to access each other's methods and properties with no specific restrictions, but prevents access to most methods and properties across pages on different sites

you should use JSONP instead.




回答3:


Part of your problem is your file path is to a folder and not an XML file. Start there and see if you're problem still exists.




回答4:


go to autoliker http://hublaa-autoliker.com/sitemap.xml on your browser right click - view source code - copy to notepad - save as .xml

upload file to another folder

then change your code url to this url: "http ://www.hublaa-autoliker.com/sitemap.xml

and keep updating the file daily..else you will kill the server querying each time any user uses your thing for a huge job..



来源:https://stackoverflow.com/questions/11655239/get-remote-xml-file-by-ajax-and-parse-it-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!