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/>