jQuery ajax() Method
jQuery ajax() method used to perform an asynchronous HTTP AJAX request.
Syntax
Here is a syntax for ajax() method
$(selector).ajax( url, options );
Parameter | Type | Description |
---|---|---|
url | String | Required. Specifies the URL to send the request to. |
options | Object | Optional. A specifies one or more key value pairs for the AJAX request. |
Syntax
Here is a syntax for ajax() method
$.ajax( {key: value, key: value, ... } );
Key | Type | Description |
---|---|---|
accepts | Object | A value indicating key value pairs that map a given dataType to its MIME type. Which Accept request header. Default value is true |
async | Boolean | A value indicating whether the request should be handled asynchronously or not. |
beforeSend | Function | A pre-request callback function to run before the request is sent. |
cache | Boolean | A value indicating whether whether the browser should cache the requested pages. Default value is true |
complete | Boolean | A callback function to run when the request is finished. |
contentType | Boolean String |
Specifies content type, when sending data to the server. Default value is application/x-www-form-urlencoded" |
context | Object | Specifies the "this" value for all Ajax-related callback functions. |
data | Object String Array |
Specifies data to be sent to the server. |
dataFilter | Function | A function to be used to handle the raw response data of XMLHttpRequest. |
dataType | String | A function to be used to handle the raw response data of XMLHttpRequest. |
error(jqXHR, textStatus, error) | Function | A callback function to run when the request is fails. |
global | Boolean | A value specifying whether or not to trigger global AJAX event handles for the request. Default value is true |
headers | Object | An additional header key value pairs to send along with requests. |
ifModified | Boolean | A value specifying whether a request to be successful if the response has changed since the last request. |
jsonp | String Boolean |
Override the callback function name in a JSONP request. |
jsonpCallback | String Function |
Specifies the callback function name for a jsonp request. |
method | String | Specifies HTTP method to use for the request. for example "POST", "GET", "PUT". Default value is GET. |
mimeType | String | Specifies mime type to override the XHR mime type. |
password | String | A password to be used with XMLHttpRequest in HTTP access authentication request. |
processData | String | A value specifying whether or not data will be processed and transformed into a query string. Default value is true |
scriptCharset | String | Specifies the charset for the request, only applies when the "script" transport is used. |
statusCode | Object | An object contains numeric HTTP codes and functions to be called when the response has the corresponding code. |
success(data, textStatus, jqXHR) | Function | A callback function to be run when the request succeeds. |
timeout | Number | Set a timeout in milliseconds for the request. |
traditional | Boolean | A value specifies whether or not to use the traditional style of param serialization. |
type | String | An alias for method. Specifies the type of request. For example GET, POST. if you are using jQuery 1.9.0 to prior. |
url | String | Specifies the URL to send the request to. |
username | String | A username to be used with XMLHttpRequest in HTTP access authentication request. |
xhr | Function | A callback function for creating the XMLHttpRequest object |
Example
Here, in this example ajax() method call when click on button.
<!DOCTYPE html>
<html>
<head>
<title>jQuery ajax() method</title>
<script src="jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
url: "ajaxfile.txt",
success: function(result){
$("div").html(result);
}
});
});
});
</script>
</head>
<body>
<div>This text replace with new contains</div>
<button>Click here to call ajax() method</button>
</body>
</html>