Another method for Ajax cross-origin access to XML data-use the YQL query statement, ajaxyql

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.