Get Xml Data Cross-Domain with JQuery
Is there a way to load XML Data Cross-Domain with JQuery (= client side)?
$.get('http://otherdomain.com/data.xml', function(xml) { }
The above doesn't work - do I have to re开发者_运维技巧ly on JSONP, or is there a way to load XML?
No way to load XML. So yes, using "standard" xmlhttprequests you need JSONpadding.
If the server supports "Cross-Origin Resource Sharing (CORS)" you can easily receive data from a foreign domain. That means, you have to add a custom header into your XmlHttpRequest ('Origin') and the server decides if it does allow the request or not.
You need to use JSONP.
I have found that with Chrome some jsonp techniques don't work.
Either you choose json and get a cross domain error or jsonp and xml format is not recognized, as waiting for jsonp format.
So, the solution is to use Yahoo Query Language api to retrieve the data on all browsers without cross domain error.
There are lots of ways to call the link and play with the xml data.
I can't post the code I have, but you will easily find snippets on the web.
So you have to define your url to query yahoo api with encodeURI method. The getJSON method comes from jQuery and then you parse an array of text results with raw xml.
Well we are easily can get data from some php functions like file_get_content and curl (as I Php/ROR dev ,other languages has the same functions). Using these functions we can get the xml data from foreign domain,
Now question is why we are not able to do the same thing when using AJAX, As per my understanding curl and file_get_content function doesn't invoke the http request and Ajax does.
In nutshell we can not load the xml data using AJAX.
精彩评论