Deep understanding of jquery cross-domain request method _jquery

Source: Internet
Author: User
Tags error handling script tag

The use of Ajax Jsonp in the project,

A problem has occurred: The request result can be successfully obtained, but the success method is not executed

It's done, record it.

function Testajax () {$.ajax ({type: "Get", Async:false, url: "Ajaxhandler.ashx",//the address actually generated when the access is: Ajax . ashx?callbackfun=jsonpcallback&id=10 data: {id:10}, Cache:false,///default value True DataType: "Jsonp", J SONP: "Callbackfun",//passed to the request handler or page to obtain the parameter name of the JSONP callback function name (default: Callback) Jsonpcallback: "Jsonpcallback",//Custom JSONP back Call Function name, default to jquery automatically generated random function name//If the JSONP callback function is set here, the success function does not work; otherwise, success will work success:function (JSON) {Aler
    T (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 {FALSE;} }

Ajax Request Parameter Description:

DataType the data type returned by the string server.

If not specified, JQuery will automatically intelligently judge against HTTP packet mime information, such as XML MIME types being recognized as XML.

Available values:

"XML": Returns an XML document that can be processed with jQuery.

HTML: Returns plain text HTML information, and the included script tag executes when the DOM is inserted.

Script: Returns the plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set.

Note: On remote requests (not in the same domain), all post requests are converted to get requests. (because it will be loaded using the DOM's script tag)

"JSON": Returns JSON data.

' Text ': Returns a plain text string

"JSONP": Jsonp format. When calling a function using the Jsonp form,

When you access the URL, the URL is automatically appended with the following as "Callback=callbackfunname" to perform the callback function (Callbackfunname).

Jsonp string

Overrides the name of the callback function in a JSONP request. This value is used to replace the "callback=?" The "callback" part of the URL parameter in the GET or POST request,

Like Jsonp: ' Callbackfun ' will generate "callbackfun=?" passed to the server.

Jsonpcallback String This parameter specifies a callback function name for the JSONP request.

This value will be used to replace the random function name that jquery automatically generates. The "callback=" above? Part of the question mark in

This is primarily used to allow jquery to generate unique function names, so that the request is easier and can easily provide callback functions and error handling.

You can also specify this callback function name when you want the browser to cache a GET request.

The main difference between Ajax Jsonp and ordinary AJAX requests is the processing of request response results. The response, as shown in the code above, is:

Jsonpcallback ({name: ' World ', message: ' Hello World '});

is actually calling the JSONP callback function Jsonpcallback and passing in the string or JSON that will respond to this method,

On a custom JSONP callback function, the success function does not work

Grand the underlying implementation (this is, of course, the default callback function, otherwise it will not execute the Success method):

function Default_jsonpcallback (data)
{
  success (data);//execute in Default callback method
}

The last simpler method,

$.getjson ("getuserbyname.aspx?name=ww&callback=?",
function (date)
{
//...
}
)

The above in-depth understanding of the jquery cross-domain request method is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.