jQuery ajax()Method
Examples
Use an AJAX request to change the text of a <div> element:
$("button").click(function(){
$.ajax({url:"demo_test.txt",success:function(result){
$("#div1").html(result);
}});
});
Try it Yourself »
Definition and Usage
The ajax() method is used to perform an AJAX (asynchronous HTTP) request.
All jQuery AJAX methods use the ajax() method. This method is usually used for requests that other methods cannot complete.
Syntax
$.ajax({name:value, name:value, ... })
This parameter specifies one or more name/value pairs for the AJAX request.
The following table lists the possible name/value pairs:
| Name | Value/Description |
|---|---|
| async | A Boolean value indicating whether the request is handled asynchronously. Default is true. |
| beforeSend(xhr) | A function to run before the request is sent. |
| cache | A Boolean value indicating whether the browser caches the requested page. Default is true. |
| complete(xhr,status) | A function to run when the request completes (called after both success and error functions, i.e., after the success and error functions). |
| contentType | The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded". |
| context | Specifies the "this" value for all AJAX-related callback functions. |
| data | Specifies the data to be sent to the server. |
| dataFilter(data,type) | A function for processing 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 value specifying whether to trigger global AJAX event handlers for the request. Default is true. |
| ifModified | A Boolean value specifying whether the request is only successful if the response has changed since the last request. Default is false. |
| jsonp | A string for overriding the callback function in a jsonp. |
| jsonpCallback | Specifies the name of the callback function in a jsonp. |
| password | Specifies the password to use in an HTTP access authentication request. |
| processData | A Boolean value specifying whether the data sent via the request is converted to a query string. Default is true. |
| scriptCharset | Specifies the character set for 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 value specifying whether to use the traditional style of parameter serialization. |
| type | Specifies the type of request (GET or POST). |
| url | Specifies the URL to send the request to. Default is the current page. |
| username | Specifies the username to use in an HTTP access authentication request. |
| xhr | A function for creating the XMLHttpRequest object. |
More Examples
Create an asynchronous AJAX request
How to use the async setting to specify an asynchronous request.
Create an AJAX request with a specified data type
How to use the dataType setting to specify the data type of the request.
Create an AJAX request with errors
How to use the error setting to handle errors in AJAX requests.
Other Extensions
jQuery AJAX Methods