In-depth understanding of jquery cross-origin request methods and jquery cross-Origin
Use of ajax jsonp in the project,
A problem occurs: The request result can be obtained successfully, but the success method is not executed.
It's done. record it.
Function TestAjax () {$. ajax ({type: "get", async: false, url: "ajaxHandler. ashx ", // the actual address generated during access is ajax. ashx? Callbackfun = jsonpCallback & id = 10 data: {id: 10}, cache: false, // default value: true dataType: "jsonp", jsonp: "callbackfun ", // The parameter name passed to the request handler or page to obtain the jsonp callback function name (callback by default) jsonpCallback: "jsonpCallback ", // The Custom jsonp callback function name. The default value is the random function name automatically generated by jQuery. // If the jsonp callback function is customized here, the success function does not work; otherwise success will take effect success: function (json) {alert (json. message) ;}, error: function () {alert ("erroe") ;}}) ;}function jsonpCallback (data) // callback function {alert (data. message); //} public class ajaxHandler: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; string callbackfun = context. request ["callbackfun"]; context. response. write (callbackfun + "({name: \" John \ ", message: \" hello John \ "})"); context. response. end () ;}public bool IsReusable {get {return false ;}}
Ajax request parameters:
The data type returned by the dataType string server.
If this parameter is not specified, jQuery automatically determines Based on the MIME information of the HTTP package. For example, the xml mime type is recognized as XML.
Available value:
"Xml": returns an XML document, which can be processed by jQuery.
"Html": returns plain text HTML information. The script tag is executed when the dom is inserted.
"Script": returns plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set.
Note: During remote requests (not in the same domain), all POST requests are converted to GET requests. (Because the script tag of DOM will be used for loading)
"Json": Return JSON data.
"Text": returns a plain text string.
"Jsonp": jsonp format. When calling a function in the form of jsonp,
When accessing a url, the system automatically adds "callback = callbackFunName" after the url to execute the callback function (callbackFunName ).
Jsonp string
Rewrite the name of the callback function in a jsonp request. This value is used to replace "callback =? "Callback" in the url parameter in this get or post request,
For example, jsonp: 'callbackfun' will generate "callbackfun =? "To the server.
JsonpCallback String this parameter specifies a callback function name for a jsonp request.
This value is used to replace the random function name automatically generated by jQuery. That is, the above "callback =? "Question mark Section
This is mainly used to make jQuery generate a unique function name, so that the request is easier and callback functions and error processing can be conveniently provided.
You can also specify the callback function name when you want the browser to cache the GET request.
The main difference between ajax jsonp and common ajax requests is the processing of request response results. The response result shown in the code above is:
JsonpCallback ({name: "world", message: "hello world "});
In fact, it is to call the jsonp callback function jsonpCallback and input the response string or json to this method,
The success function does not work for the callback function of jsonp.
Probably its underlying implementation (of course, this is the default callback function, otherwise the success method will not be executed ):
Function default_jsonpCallback (data) {success (data); // execute in the default callback method}
The last simple method,
$.getJSON("GetUserbyName.aspx?name=ww&callback=?",function(date){//....})
The above in-depth understanding of jquery's cross-origin request method is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's house.