In-depth understanding of $. Get, $. Post, $. getjson, and $. Ajax in jquery

Source: Internet
Author: User
Conversion from: understand $. Get, $. Post, $. getjson, and $. Ajax usage in jquery.

When we write Ajax programs in Javascript, someone suddenly tells you something called jquery, which will tell you how happy it is not directly with httprequest, at the same time, you no longer need to worry about the tangle of Ajax garbled issues. More happily, your JS Code will be greatly simplified. After reading this article, you will find that AJAX, simply put, it is a sentence.

This article focuses on four methods for calling Ajax in jquery: $. Get, $. Post, $ getjson, and $ Ajax. If you do not have any knowledge about JavaScript or jquery, or you do not have any idea about Ajax, ask Google boss and read this article.

1. $. Get

$. Get () method uses the get Method for asynchronous requests. Its syntax structure is:

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

Explain the parameters of this function:

URL: string type, the URL of the Ajax request.

Data: optional parameter, object type. The key/value data sent to the server is appended to the request URL as querystring.

Callback: an optional parameter of the function type. This function is automatically called when Ajax returns a successful result.

Finally, write an instance of $. Get () for your reference:

$. Get ("Submit. aspx ", {ID: '000000', name: 'qingteng Garden ',}, function (data, state) {// The data returned from the server alert (data) is displayed here ); // The returned status alert (State);} is displayed here );})

2. $. Post ()

$. Post () method uses the POST method for asynchronous requests. Its syntax structure is:

$. Post (URL, [data], [callback], [type])

This method is similar to $. Get (), but only one type parameter is added. Here we will only introduce the type parameter. For more information, see $. Get () above.

Type: type indicates the request data type, which can be HTML, XML, JSON, and Other types. If we set this parameter to JSON, the returned format is JSON, and $. the format returned by get () is the same as that returned by get.

Finally, write a $. Post () instance for your reference:

$. Post ("Submit. aspx ", {ID: '000000', name: 'qingteng Garden ',}, function (data, state) {// The data returned from the server alert (data) is displayed here ); // The returned status alert (state) ;}, "JSON") is displayed ")

3. $. getjson ()

$. Getjson () is specially set for Ajax to obtain JSON data and supports cross-origin calls. Its Syntax format is:

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

URL: string type, sending request address
Data: an optional parameter. The key/value parameter to be sent. It is of the same get or post type.
Callback: an optional parameter. It is the callback function when loading successfully. It is of the same get or post type.

JSON is an ideal data transmission format. It can be well integrated with JavaScript or other host languages and can be directly used by Js. JSON is more reasonable and safer in structure than the traditional method of sending "naked" data directly through get and post. For jquery's getjson () function, it is only a simplified version of AJAX () function with JSON parameters. This function can also be used across domains. It has some advantages over get () and post. In addition, this function can write the request URL as "myurl? Callback = x ", which allows the program to execute the callback function x.

4. $. Ajax ()

$. Ajax () is a common Ajax encapsulation in jquery. Its Syntax format is:

$. Ajax (options)

Options is an object type, which specifies the specific parameters of this Ajax call. Here I attach the most common parameters

 
$. Ajax ({URL: 'submit. aspx ', datatype: "JSON", type: 'post', success: function (e) {// callback after successful alert (e) ;}, error: function (E) {// callback alert (e) ;}, beforesend: function () {/called before sending the request, alert ("loading ");}})

Well, the above are several methods for jquery to implement Ajax calls. I hope this will be helpful to you.

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.