Cross-domain access agent Api-yahooapis

Source: Internet
Author: User
Tags json

Do you have a problem with using AJAX to access some public-network APIs, but you don't want to set up your own proxy service, because sometimes I don't plan to involve any of the server's code, but the annoying browser's Sanyuan strategy blocks our Ajax calls.

For example I would like to visit a weather Restfull API if I go straight to get:

$.get ("http://m.weather.com.cn/data/101010100.html");

See this problem believe that everyone will not be unfamiliar, will naturally get the solution, but I really do not want to touch any service-side code, with Jsonp bar, but the server did not implement the contract.

It's time for me to introduce the main character Yahoo provides JSONP agent: HTTP://QUERY.YAHOOAPIS.COM/V1/PUBLIC/YQL

Implementing Cross-domain Access code is: http://jsfiddle.net/whitewolf/4UDpf/9/

Html:

<script type= "Text/javascript" src= "Http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js" ></script >   
<center id= "Content" >   
</center>   
JS:   
$ (function () {   
    $.getjson ("http:// Query.yahooapis.com/v1/public/yql ", {   
    Q:" SELECT * from JSON where url= "http://m.weather.com.cn/data/101010100. HTML "",   
    Format: "JSON"   
}, function (data) {   
    var $content = $ ("#content")   
    if (data.query.results) {   
        $content. Text (json.stringify (data.query.results));   
    else {   
        $content. Text (' No such code: ' + code ';}});   

Effect:

Much needless to say, the principle of jsonp I believe that we are also very clear.

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.