The $. Ajax method of jquery can be used to control Ajax requests in more detail. It applies fine-grained control to Ajax requests.
$. Ajax method syntax
$. Ajax (options) |
Parameters |
|
Options |
An instance of an object. Its Properties define the parameters of this operation. For more information, see the table below. |
Return Value |
Xhr instance |
Options detailed range value
Name |
Type |
Description |
URL |
String |
Requested URL |
Type |
String |
The HTTP method to be used. Usually post or get. If omitted, the default value is get. |
Data |
Object |
An object whose attributes are used as query parameters to pass requests. For a GET request, data is transmitted as the query string; for a POST request, data is transmitted as the request body. In both cases, the $. Ajax () Utility Function processes the value encoding. |
Datatype |
String |
A keyword that identifies the type of data that is expected to be returned by the response. This value determines what subsequent processing will be performed before the data is passed to the callback function (if any. Valid values: XML-the response text is parsed as an XML document, and the xml dom as the result is passed to the callback function. HTML-response text is passed to the callback function without being processed. Any <SCRIPT> block in the returned HTML segment will be evaluated JSON-the response text is evaluated as a JSON string, and the result object is passed to the callback function. Jsonp-similar to JSON, the difference is to provide remote script support (assuming Remote Server Support) Script-the response text is passed to the callback function. Before any callback function is called, the response is processed as one or more JavaScript statements. Text-the response text is assumed to be plain text. Server resources are responsible for setting appropriate Content-Type response headers. If this attribute is omitted, the callback function will not process or evaluate the response text. |
Timeout |
Value |
Set the Ajax request timeout value (MS ). If the request is not completed before the timeout value expires, stop the request and call the error callback function (if defined) |
Global |
Boolean |
Enable or disable the triggering of global functions. These functions can be appended to elements and triggered at different times or states of Ajax calls. Global function triggering is enabled by default. |
Contenttype |
String |
The content type to be specified on the request. The default value is application/X-WWW-form-urlencoded (same as the default type used for form submission) |
Success |
Function |
If the request response indicates a success status code, this function is called. The response body is returned to this function as the first parameter and is performed according to the specified datatype attribute. The second parameter is a string containing a status code-in this case, it is always a success status code. |
Error |
Function |
If the request response returns an error code, this function is called. Three real parameters are passed to this function: xhr instance, status message string (in this case, it is always an error Status Code), and the exception object returned by the xhr instance (optional) |
Complete |
Function |
Called when the request is complete. Two real parameters are passed: xhr instance and status message string (success status code or error Status Code ). If the success or error callback function is also specified, this function is called after the success or error callback function is called. |
Beforesend |
Function |
Called before initiating a request. This function is passed to the xhr instance and can be used to set custom headers or perform other pre-request operations. |
Async |
Boolean |
If this parameter is set to false, the request is submitted as a synchronization request. By default, requests are asynchronous. |
Processdata |
Boolean |
If it is set to false, the transmitted data is prevented from being processed as the URL encoding format. By default, data is processed in URL encoding format (applicable to requests of application/X-WWW-form-urlencoded type) |
Ifmodified |
Boolean |
If it is set to true, the request is allowed to succeed only when the response content has not changed since the previous request (based on the last-modified header. If this parameter is omitted, no header check is performed. |
The following example shows how to use as many options as possible.
client Code :
<HTML xmlns = "http://www.w3.org/1999/xhtml">
Main server code:
Protected void page_load (Object sender, eventargs e) {If (! Page. ispostback) {If (request ["ID"]! = NULL &&! String. isnullorempty (request ["ID"]. tostring () {// enabling this sentence will cause an Ajax timeout exception // system. threading. thread. sleep (3000); response. write (getdata (request ["ID"]. tostring () ;}} protected string getdata (string ID) {string STR = string. empty; Switch (ID) {Case "1": Str + = "this is number 1"; break; Case "2": Str + = "this is number 2 "; break; Case "3": Str + = "this is number 3"; break; default: Str + = "Warning oth Er number! "; Break;} return STR ;}
RunProgram, Result
Download demo