Detailed usage of $.get, $.post, $.getjson and $.ajax in jquery _jquery

Source: Internet
Author: User

When we write the Ajax program with JavaScript "Happy", suddenly someone tells you there is a thing called jquery, it will tell you not directly and HttpRequest is how happy, at the same time you no longer need to worry about the tangled Ajax garbled problem, Happier is your JS code will be greatly simplified, after reading this article, you will find that Ajax, simply speaking is a word of things.

1, $.get

The $.get () method uses the Get method to make an asynchronous request, and its syntax structure is:

$.get (URL [, data] [, callback])

Explain the parameters of this function:

Url:string type, the address of the AJAX request.

Data: Optional parameter, object type, Key/value data sent to the server is appended to the request URL as a querystring.

Callback: Optional parameter, function type, which is called automatically when the Ajax return succeeds.

Finally write an example of $.get () for your reference:

Copy Code code as follows:

$.get (
"Submit.aspx", {
ID: ' 123 ',
Name: ' Green rattan garden ',
},function (data,state) {
This shows the data returned from the server
alert (data);
This shows the status of the returned
alert (state);
}
)

2, $.post ()

The $.post () method uses the Post method to make an asynchronous request, and its syntax structure is:

$.post (Url,[data],[callback],[type])

This method is similar to $.get (), but only one type parameter, so here is only the type parameter, other references above $.get ().

Type:type for the requested data type, can be html,xml,json and so on, if we set this argument to: JSON, the returned format is in JSON format, and if there is no setting, it is a string, just like the format returned by $.get ().

Finally write an example of $.post () for your reference:

Copy Code code as follows:

$.post (
"Submit.aspx", {
ID: ' 123 ',
Name: ' Green rattan garden ',
},function (data,state) {
This shows the data returned from the server
alert (data);
This shows the status of the returned
alert (state);
},
"JSON"
)

3, $.getjson ()

The $.getjson () is set specifically for capturing JSON data for Ajax and supports Cross-domain calls with syntax in the form of:

Getjson (Url,[data],[callback])

Url:string type, send request address data: Optional parameters, to be sent Key/value parameters, with the Get,post type of data callback: Optional parameters, load callback function when successful, get,post with callback type

JSON is an ideal data transmission format that blends well with JavaScript or other host languages and can be used directly by JS. Using JSON to send "naked" data directly via get and post is more logical and more secure. As for the Getjson () function of jquery, just a simplified version of the Ajax () function that sets the JSON parameter. This function can also be used across domains, compared to get (), post () has some advantages. In addition, this function allows the program to execute the callback function X by writing the request URL in "myurl?callback=x" format.

4, $.ajax ()

$.ajax () is a common Ajax encapsulation in jquery, and its syntax is in the form of:

$.ajax (Options)

Where the options are an object type, which indicates the specific parameters of this Ajax call, here I enclose the most commonly used parameters

Copy Code code as follows:

$.ajax ({
URL: ' submit.aspx ',
DataType: "JSON",
Type: ' Post ',
Success:function (e) {//after successful callback
Alert (e);
},
Error:function (e) {//callback after failure
Alert (e);
},
Beforesend:function () {/called before sending the request, you can put some "loading" words
Alert ("Loading");
}
})

The above is jquery implementation of several Ajax call methods, Ajax call is quite complex, I hope this article can help everyone, if there is any doubt, you can contact me, we common progress.

Related Article

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.