Ajax cross-domain same-origin policy limitation

Source: Internet
Author: User

From: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

 

Ajax same-origin policy (SOP) Limitation:

 

Ajax prevents cross-domail invokation, there are several ways to by pass this limitation.

 

1. Write a proxy on the server side. The SOP limitation only exists only on the Javascript side. While on the side, we can still invoke the other domail URL such as via httpclient

 

2. IFRAME (not sure)

 

3. jsonp (JSON with padding)

 

The same-origin policy doesn't prevent the insertion of dynamic script elements into the document. That is, you cocould dynamically insert JavaScript from different domains, carrying JSON data in them.

 

<MCE: Script Type = "text/JavaScript"> <! -- <Br/> // This is our function to be called with JSON data <br/> function showprice (data) {<br/> alert ("Symbol:" + data. symbol + ", price:" + data. price); <br/>}< br/> var url = "ticker. JS "; // URL of the external script <br/> // This shows dynamic script insertion <br/> var script = document. createelement ('script'); <br/> script. setattribute ('src', URL); </P> <p> // load the script <br/> document. getelementsbytagname ('head') [0]. appendchild (SCRIPT); <br/> // --> </MCE: SCRIPT>

 

 

Note that, in order to do this, you must have a callback function already defined in the web page at the time of insertion.

 

Beginning with version 1.2, jquery has had native support for jsonp CILS. you can load JSON data located on another domain if you specify a jsonp callback, which can be done using the following syntax:url?callback=?.

 

Ajax invoke:

Jquery. getjson ("http://www.yourdomain.com/jsonp/ticker? Symbol = IBM & callback =? ", <Br/> function (data) {<br/> alert (" Symbol: "+ data. symbol + ", price:" + data. price); <br/>}); <br/>

 

 

Another domain generates JSON data and returned to client side with callback function.

@ Override <br/> protected void doget (httpservletrequest req, httpservletresponse resp) <br/> throws servletexception, ioexception {<br/> string jsondata = getdataasjson (req. getparameter ("symbol"); <br/> string output = req. getparameter ("Callback") + "(" + jsondata + ");"; </P> <p> resp. setcontenttype ("text/JavaScript"); </P> <p> printwriter out = resp. getwriter (); <br/> out. println (output); <br/> // prints: jsonp1232617942575 ({"symbol": "IBM", "price": "91.42 "}); <br/>}< br/>

 

 

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.