jQuery ajax() method
Example
Change using an AJAX request
$
(
"
button
"
)
.
click
(
function
(
)
{
$.
ajax
(
{
url
:
"
demo_test.txt
"
,
success
:
function
(
result
)
{
$
(
"
#div1
"
)
.
html
(
result
)
;
}
}
)
;
}
)
;
Try it »
Definition and Usage
The ajax() method is used to perform AJAX (asynchronous HTTP) requests.
All jQuery AJAX methods use the ajax() method. It is typically used for requests that other methods cannot handle.
Syntax
$.ajax(
{name:value, name:value, ... }
)
This parameter specifies one or more name/value pairs for the AJAX request.
The table below lists the possible name/value pairs:
| Name | Value/Description |
|---|---|
| async | A Boolean indicating whether the request is processed asynchronously. The default is true. |
| beforeSend(xhr) | A function to run before the request is sent. |
| cache | A Boolean indicating whether the browser caches the requested page. The default is true. |
| complete(xhr,status) | A function to run when the request completes (called after the request succeeds or fails, that is, after the success and error functions). |
| contentType | The content type used when sending data to the server. The default is: "application/x-www-form-urlencoded". |
| context | Specifies the "this" value for all AJAX-related callback functions. |
| data | Specifies the data to send to the server. |
| dataFilter(data,type) | A function used to process the raw response data of the XMLHttpRequest. |
| dataType | The expected data type of the server response. |
| error(xhr,status,error) | A function to run if the request fails. |
| global | A Boolean specifying whether to trigger global AJAX event handlers for the request. The default is true. |
| ifModified | A Boolean specifying whether the request succeeds only if the response has changed since the last request. The default is false. |
| jsonp | A string that overrides the callback function in a jsonp request. |
| jsonpCallback | Specifies the name of the callback function in a jsonp request. |
| password | Specifies the password to use in an HTTP access authentication request. |
| processData | A Boolean specifying whether the data sent with the request is converted into a query string. The default is true. |
| scriptCharset | Specifies the character set of the request. |
| success(result,status,xhr) | A function to run when the request succeeds. |
| timeout | Sets the local request timeout (in milliseconds). |
| traditional | A Boolean specifying whether to use the traditional style of parameter serialization. |
| type | Specifies the type of the request (GET or POST). |
| url | Specifies the URL to send the request to. The default is the current page. |
| username | Specifies the username to use in an HTTP access authentication request. |
| xhr | A function used to create the XMLHttpRequest object. |
More examples
Generate an asynchronous AJAX request
How to use the async setting to specify an asynchronous request.
Generate an AJAX request with a specified data type
How to use the dataType setting to specify the type of the request's data.
Generate an AJAX request with an error
How to use the error setting to handle errors in an AJAX request.
jQuery AJAX methods