Turn from: http://www.open-open.com/lib/view/open1334026513327.html
Today in the project to do the remote data loading and rendering the page, until the development phase to realize the problem of Ajax Cross-domain request, vaguely remember jquery has mentioned an AJAX Cross-domain request solution, so immediately out of the jquery API out of the study, Discover that jquery has two types of solutions for Ajax Cross-domain requests, but only the get way is supported. Is the jquery jquery.ajax jsonp format and the Jquery.getscript method respectively.
What is the JSONP format? Original API: If you get a data file that resides on a remote server (the domain name is different, that is, cross domain to get data), you need to use the JSONP type. Using this type, a query string parameter is created callback=? , this parameter is appended to the URL of the request. The server side should precede the JSON data with the callback function name in order to complete a valid JSONP request. This means that the remote server needs to process the returned data and return a callback (JSON) data based on the callback parameters submitted by the client, and the client will process the returned data in a script to handle the JSON data. The Jquery.getjson also supports the JSONP data method invocation.
Sample call code for client Jquery.ajax:
$.ajax ({
type: "Get",
async:false,
URL: "http://www.xxx.com/ajax.do",
dataType: "Jsonp"
, JSONP: "Callbackparam",//server is used to receive the parameters of the function name of the callback call
jsonpcallback: "Success_jsonpcallback",// callback function name
success:function (JSON) {
alert (JSON);
alert (json[0].name);
},
error:function () {
alert (' fail ');
}}
);
Sample code for server-side return data:
public void ProcessRequest (HttpContext context) {context
. Response.ContentType = "Text/plain";
String Callbackfunname = context. request["Callbackparam"];
Context. Response.Write (Callbackfunname + "([{name:\" john\ "}])");
Other reference Links:
http://justcoding.iteye.com/blog/1366102
http://tonl.iteye.com/blog/1682394