1.AJAX () function
Of Ajax () function specifies two parameters. The first parameter specifies the target URL. The second parameter is a correlation parameter is assigned, the object information types or HTTP callback functions used and the like. It may be omitted first parameter, and the URL specified as the attribute of the second object parameter.
AJAX operation performed by jQuery
$.ajax('/foo', {
type: 'GET',
success: function (data, status, xhr) {
// 成功时将执行的处理
},
error: function (xhr, status, errorThrown) {
// 失败时将执行的处理
}
});
Property name | Explanation |
---|---|
url | Send the request URL targets |
type | HTTP type used |
timeout | overtime time. In milliseconds |
async | Whether to perform asynchronous communication |
crossDomain | Whether to perform cross-origin communication |
isLocal | When accessing the file system and other local environment is true |
data | Transmitted data object or string |
processData | Whether or not the data is converted to a query string is sent |
traditional | This property is used for the serialization data is converted to a query string designating used. If you specified as true, the old-fashioned way through, at the time of conversion not nested object serialization |
headers | Request headers |
ifModified | If the value is specified as true, the only request when the data is considered sent successfully changed |
cache | Whether to use the browser cache |
dataType | Specified by the data response string type. You may be designated as a xml, html, script, json and text in. Callback function will be transmitted after converting the data type specified herein |
accepts | Accept header information for the specified map values. Which is a key dataType, Accept header information value is designated |
mimeType | Forced to override the value of Content-Type header information |
contents | Regular expression when mapping to determine the type of the response data via the Content-Type header information is used (which is a key dataType, which is a regular expression) |
converters | Mapping function for analyzing the response data. Whose key is a string before conversion and the converted data type is connected to a space formed (if the html text is converted to "text html"), a value of the function |
context | this in the callback function referenced objects |
beforeSend(xhr, settings) | Callback function to execute before sending. If the function returns false, then the cancellation request is sent |
success(data, status, xhr) | The callback function callback function error (xhr, status) executed when successful communication is executed when a communication failure |
complete(xhr, status) | Callback function is executed when communication is completed. It will also perform in the success or failure |
dataFilter(data, type) | Response data for the filter callback function. The function () is executed prior to the success, the result as data parameter passed to Success () |
statusCode | The mapping for specifying a callback function for each status code. Which is a key status code, a value of the function |
jsonp | Parameter specifies the name of the callback function name request transmitted JSONP used. If not specified, the default parameter called callback |
jsonpCallback | Callback function name JSONP request. If not specified, it is automatically set |
scriptCharset | Specify the character set used when reading the script. DataType only when the script is valid or jsonp |
global | Whether to trigger a global event related to AJAX |
xhr | Factory for creating the XMLHttpRequest object function |
xhrFields | Map XMLHttpRequest object attribute set |
username | In the name of the user authentication is required to access used in the |
password | In the authentication is required to access the password used in |
2.AJAX () wrapper function
function | Explanation |
---|---|
get(url, [data,] [success(data, status, xhr)] [dataType]) | Communicate via the GET method. You can specify the data type of data transmitted, successful callback function, and response of |
post(url, [data,] [success(data, status, xhr)] [dataType]) | Communicate via a POST. Parameters and get () function is the same as |
getJSON(url, [data,] [success(data, status, xhr)]) | JSON data acquired by the GET method. Data can be transmitted, when the communication is successful callback specified |
getScript(url, [success(data, status, xhr)]) | JavaScript acquired by the GET method and execution. Can specify a callback function when communication is successful |
Can be changed by ajaxSetup () function ajax () function can set the default value of the option
3. Global event
When using ajax () function will trigger multiple events. Because these events are valid for any element, so called global event. If you do not want to trigger a global event, you need to ajax () function of the global property option is set to false.
Method name | Execution timing listeners |
---|---|
ajax start (func ()) | When AJAX communication starts. Even if a plurality of communication AJAX performed simultaneously also will be executed only once |
ajaxSend(func(event, xhr, options)) | Before sending the request |
ajaxSuccess(func(event, xhr, options)) | When communication is successful |
ajaxError(func(event, xhr, options, error)) | When the communication failure |
ajaxComplete(func(event, xhr, options)) | At the end of the communication (regardless of communication success or failure) |
ajax stopper (func ()) | At the end of all AJAX communication |