The application of jquery and Ajax--"sharp jquery" (2nd edition) reading notes 3

Source: Internet
Author: User
Tags getscript

The 6th chapter the application of jquery and Ajax

jquery encapsulates Ajax operations, in jquery the $.ajax () method is the lowest-level method, the 2nd layer is the load (), $.get (), and $.post () methods, and the 3rd layer is the $.getscript () and $.getjson () methods.

1 , load () Method

(1) Loading HTML documents

The load () method is the simplest and most common Ajax method in jquery that can load remote HTML code and insert it into the DOM. It is structured as:

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

Parameter description:

URL: The URL address of the request HTML page;

Data (optional): Key/value data sent to the server, object type;

Callback (optional): The callback function when the request completes, whether the request succeeds or fails.

(2) Filter the loaded HTML document

By specifying a selector for the URL parameter, it is easy to filter out the required content from the loaded HTML document. The URL parameter of the load () method has the syntax structure: "URL selector". Notice that there is a space between the URL and the selector.

(3) Delivery mode

The load () method is passed as specified according to the parameter data. If no parameter is passed, it is passed in Get mode, and vice versa, it is automatically converted to post mode.

(4) Callback function

For operations that must be completed before loading, the load () method provides a callback function (callback) that has 3 parameters that represent the content returned by the request, the request status, and the XMLHttpRequest object.

2 , $.get () methods and $.post () Method

The load () method is typically used to obtain a static data file from a Web server. In the project, if you need to pass some parameters to a page in the server, you can use the $.get () or $.post () method (or the $.ajax () method).

The $.get () method uses the Get method to make an asynchronous request. It is structured as:

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

Parameter description:

URL: The URL address of the requested HTML page

Data: The key/value that is sent to the server is appended to the request URL as querystring.

Callback: The callback function when loading succeeds.

Type: server-side return content format, including XML, HTML, script, JSON, text, _default.

If the server side receives the data that is passed and returns successfully, the returned data can be displayed to the page through a callback function.

The callback function of the $.get () method has only two parameters, the code is as follows:

function (data, textstatus) {

The content returned by data can be XML documents, JSON files, HTML fragments, and so on

Textstatus Request Status: Success, error, notmodified, timeout 4

}

It is generally easiest to use HTML fragments to provide return data when it is not necessary to share data with other applications, and if the data needs to be reused, the JSON file is a good choice, with advantages in terms of performance and file size, and i,xml documents when the remote application is unknown. It is the "Esperanto" of the Web service domain.

The $.post () method and the $.get () method are both structured and used in the same way, but there are still some differences between them.

3 , $.getscript () methods and $.getjson () Method

jquery provides $. GetScript () method to load the. js file directly.

The $.getjson () method is used to load the JSON file.

4.$.ajax () method

The $.ajax () method is the bottom-most AJAX implementation of jquery.

It is structured as:

$.ajax (Options)

The method has only 1 parameters, but in this object contains information such as the request settings and callback functions required by the $.ajax () method, the parameter key/value is in the form, all parameters are optional.

5 , serialized elements

The serialize () method works with a jquery object, which serializes the contents of a DOM element into a string and is used for AJAX requests.

The Serializearray () method is also used for a jquery object, but, after serializing the DOM element, it returns the JSON-formatted data, which can then be used to iterate the output using the $.each () function.

The $.param () method is used to serialize an array or object according to Key/value.

6 , JQuery in the Ajax Global Events

    • Ajaxcomplete (callback) The function that is executed when the AJAX request is completed;
    • Ajaxerror (callback) The function that executes when an AJAX request error occurs, the catch error can be passed as the last parameter;
    • Ajaxsend (callback) the function executed before the AJAX request is sent;
    • Ajaxstart (callback) the function executed at the beginning of the AJAX request;
    • Ajaxstop (callback) the function executed at the end of the AJAX request;
    • Ajaxsuccess (callback) The function that executes when the AJAX request succeeds.

These methods are global in that they are triggered whenever an AJAX request occurs, regardless of where the code that created them is located.

The application of jquery and Ajax--"sharp jquery" (2nd edition) reading notes 3

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.