$. Ajax Overview

Source: Internet
Author: User
Document directory
  • Parameter List:

Jquery makes Ajax easier to implement.

In jquery, the advanced implementation of Ajax mainly includes $. Get (), $. Post (), etc. The usage of $. Ajax () is described in detail below.

1. Request Ajax. aspx

The JS Code is as follows:

<SCRIPT type = "text/JavaScript">
Function text_ajax ()
{
$. Ajax (
{
Type: "Get", // two types are usually used: Get and post. The default value is get.
URL: "responsetext. aspx", // (default: Current page address) Address of the request sent
Datatype: "html", // data type returned by the expected server.
Beforesend: beforesend, // send the request
Success: callback, // The request is successful.
Error: error, // request Error
Complete: complete // The request is complete

});
}
Function error (XMLHttpRequest, textstatus, errorthrown)
{
// Generally, textstatus and errorthown have only one value.
$ ("# Showresult"). append ("<div> request error! </Div> ");
}
Function complete (XMLHttpRequest, textstatus)
{
$ ("# Showresult"). append ("<div> request completed </div> ");
}
Function beforesend (XMLHttpRequest)
{
$ ("# Showresult"). append ("<div> send request... <div> ");

}
Function callback (MSG)
{
$ ("# Showresult"). append ("<div> Successful request, return count:" + MSG + "<div> ");
}

</SCRIPT>

The HTML code is as follows:

<Input value = "text_ajax function" type = "button" onclick = "text_ajax ()"/>
<Div id = "showresult">
</Div>

Response page: jqueryajax. aspx

Background code:

Protected void page_load (Object sender, eventargs E)
{
Response. Write ("haha! The test is successful! ");
}

Parameter List:

Parameter Name

Type

Description

URL 

String

(Default: Current page address) the address of the request sent.

Type 

String

(Default: "Get") Request Method ("Post" or "get"). The default value is "get ". Note: Other HTTP request methods, such as put and delete, can also be used, but are only supported by some browsers.

Timeout 

Number

Set the request timeout (in milliseconds ). This setting overwrites the global setting.

Async 

Boolean

(Default: True) by default, all requests are asynchronous requests. To send a synchronization request, set this option to false. Note: The synchronous request locks the browser. Other operations can be performed only after the request is completed.

Beforesend 

Function

Before sending a request, you can modify the function of the XMLHTTPRequest object, for example, adding a custom HTTP header. The XMLHTTPRequest object is a unique parameter.

Function (XMLHttpRequest ){

This; // The options for this Ajax request

}

Cache 

Boolean

(Default: True) New jquery 1.2 function. setting this parameter to false will not load request information from the browser cache.

Complete 

Function

Callback Function after the request is complete (called when the request is successful or fails ). Parameter: XMLHTTPRequest object, success information string.

Function (XMLHttpRequest, textstatus ){

This; // The options for this Ajax request

}

Contenttype 

String

(Default: "application/X-WWW-form-urlencoded") Content Encoding type when sending information to the server. The default value is applicable to most applications.

Data

Object,
String

The data sent to the server. Will be automatically converted to the request string format. The GET request will be appended to the URL. View the description of the processdata option to disable automatic conversion. It must be in key/value format. If it is an array, jquery automatically corresponds to the same name for different values. For example, {FOO: ["bar1", "bar2"]} is converted to '& Foo = bar1 & Foo = bar2 '.

Datatype

String

Expected data type returned by the server. If this parameter is not specified, jquery will automatically return responsexml or responsetext Based on the mime information of the http package and pass it as a callback function parameter. Available values:

"XML": returns an XML document, which can be processed by jquery.

"Html": returns plain text HTML information, including script elements.

"Script": returns plain text JavaScript code. Results are not automatically cached.

"JSON": Return JSON data.

"Jsonp": jsonp format. When calling a function in the form of jsonp, such as "myurl? Callback =? "Will jquery be replaced automatically? For the correct function name to execute the callback function.

Error

Function

(Default: automatically determines (XML or HTML) This method is called when a request fails. This method has three parameters: XMLHTTPRequest object, error message, and (possibly) captured error object.

Function (XMLHttpRequest, textstatus, errorthrown ){

// Generally, textstatus and errorthown have only one value.

This; // The options for this Ajax request

}

Global

Boolean

(Default: True) Whether to trigger a global Ajax event. Setting false does not trigger global Ajax events, such as ajaxstart or ajaxstop. Can be used to control different Ajax events

Ifmodified

Boolean

(Default: false) obtain new data only when the server data changes. Use the last-modified header information of the HTTP packet to determine.

Processdata

Boolean

(Default: True) by default, data sent is converted to an object (technically not a string) with the default content type "application/X-WWW-form-urlencoded ". If you want to send the DOM tree information or other information that does not want to be converted, set it to false.

Success

Function

Callback Function after successful request. This method has two parameters: the server returns data and returns the status.

Function (data, textstatus ){

// Data cocould be xmldoc, jsonobj, HTML, text, Etc...

This; // The options for this Ajax request

}

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.