Another method for Ajax cross-origin access to XML data-use the YQL query statement, ajaxyql
XML data cannot be read through Ajax cross-origin requests on the client by default. The general practice is to write a simple proxy program on the server to read the remote XML data to the local server first, then the client requests from the local server via Ajax. Because we cannot set or modify the environment where the data source is located, it is difficult to bypass this issue only through the client code. However, if the requested data is not XML but a JSON object or JavaScript function, the JSONP method can be easily used to directly callJQuery. getJSON ()You can obtain the returned results in the callback function. To use JSONP, You can append the query parameter to the specified URL"& Callback =?".
$(document).ready(function() { $.getJSON("http://www.example.com/getdata", function(data) { console.log(data); })});
$(document).ready(function() { $.getJSON("http://www.example.com/getdata2?callback=?", function(data) { console.log(data); })});
In addition, we can use APIs provided by third-party platforms to access data. YQL is Yahoo! Provides a set of Web services that allow you to access any data on the Internet like SQL statements without cross-origin issues.
//sample site that returns xmlvar site = 'http://feed.cnblogs.com/blog/u/53608/rss';var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';// Request that YSQL string, and run a callback function.// Pass a defined function to prevent cache-busting.$.getJSON(yql, function (data) { console.log(data.results[0]);});
The public access limit for the yql api is 2000 requests per hour for the same IP address, you can view the official website's introduction https://developer.yahoo.com/yql/guide/usage_info_limits.html
However, I personally think this method is still a temporary solution. For a complete set of Web applications, too much reliance on third-party systems will directly lead to system stability and scalability, in addition, there will be great risks. Imagine if Yahoo! If you modify the Web Services interface or stop the public access to YQL, all the parts in the system that depend on YQL may have problems.