A Brief Introduction to ajax cross-origin requests and a brief introduction to ajax
A Brief Introduction to ajax cross-origin requests:
This chapter describes ajax cross-origin requests. The content mainly refers to an article on the internet, hoping to help you.
1. jsonp format file:
If the obtained data file is stored on a remote server (the domain name is different, that is, cross-Origin data retrieval), The jsonp type is required. If this type is used, a query string parameter callback =? will be created? This parameter is added after the request URL. The server should add the callback function name before the JSON data to complete a valid JSONP request. This means that the remote server needs to process the returned data and return a callback (json) data according to the callback parameter submitted by the client, the client will process the returned data in script Mode to process json data. JQuery. getJSON also supports jsonp data calling.
Ii. Sample Code for calling the client JQuery. ajax:
$. Ajax ({type: "get", async: false, url: "http://www.softwhy.com/ajax.do", dataType: "jsonp", jsonp: "callbackparam ", // The parameter jsonpCallback of the function name used by the server to receive callback calls: "success_jsonpCallback", // The function name success of callback: function (json) {alert (json ); alert (json [0]. name) ;}, error: function () {alert ('fail ');}});
Iii. Sample Code returned by the server:
public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; String callbackFunName = context.Request["callbackparam"]; context.Response.Write(callbackFunName + "([ { name:\"John\"}])"); }
The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 8131.
For more information, see: http://www.softwhy.com/jquery/