Jsonp of cross-domain requests

Source: Internet
Author: User

There are many different ways of pleading across domains,

1,iframe

2,document.domain

3,window.name

4,script

5,xdomainrequest (ie8+)

6,xmlhttprequest (firefox3.5+)

7,postmessage (HTML5)

8, background Agent

...

They have their own pros and cons, back to the data pattern is also different, should be carefully selected according to needs. For example, the IFRAME back to the HTML fragment is appropriate, the old strength to use it back to JSON pound foolish. This beginning I will build a useful cross-domain pleading thing sjax. The biggest disadvantage of using script pleading is that the challenge is to deal with the fault. For example 404 fault, it does not like XMLHttpRequest can accurately return to the condition code 404. I put this in the final article.


This series first depicts the above enumerated method 4, that is, through script back JSON pattern data data. This method is now called JSONP. JSON is one of the most prevalent and widespread patterns of communication at present and before and after the station. Compared to the previous Ajax back XML (x in AJAX is XML), JSON appears more lightweight, there is no remaining tag symbol, parsing is also native. XML goes back to the front end and turns into a document, parsing through the DOM API layer. Parsing DOM is a big expense, especially in the early version of IE (IE6/7/8), the value of core JS and Dom communication is very large.


The idea of JSONP is very simple.

1, the front-end creation of the script symbol, set SRC, added to the head (you can add to the body).

2, backstage back a JS variable jsonp, this JSONP is the request after the JSON data.

3, delete the script symbol after the callback is completed (there are some housekeeping tasks such as preventing some browser memory leaks, etc.).


Interface

Sjax.load (

url,//cross the Pleading URL

Success,//callback function, it is necessary to define a formal parameter, used to accept back the background of the global variable Jsonp (about good backstage back such as Jsonp = {...} Structure

timestamp,//true adds a timestamp, prevents caching, and implicitly does not add

);

<! DOCTYPE html>

<meta charset= "Utf-8" >

<title>sjax_0.1.js by snandy</title>

<script src= "/js/sjax_0.1.js" ></script>

<body>

<p id= "P1" style= "background:gold;" ></p>

<input type= "button" value= "Get Name" onclick= "CLK ()"/>

<script type= "Text/javascript" >

function CLK () {

Sjax.load (

'http://lf.yunnanw.cn/',

'http://www.lcsyt.com/',

'http://www.minnan888.net/',

'http://www.vipfuxin.com/',

'http://www.qclchina.com/',

'http://www.tongxinglong.com/',

'http://www.jinanwuliangye.com/',

‘‘,

function () {

document.getElementById (' P1 '). InnerHTML = ' Hi, ' + jsonp.name;

}

);

}

</script>

</body>



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.