English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
$ .ajax()方法执行异步HTTP(Ajax)请求。
$.ajax() method is used to perform asynchronous HTTP (Ajax) requests.$.ajax() is the foundation for all Ajax requests sent by jQuery. It is usually not necessary to call this method directly, as$.get(),$.post()andload()
Syntax::1.5+of the AJAX request:
Syntax One
$.ajax(url, {name:value, name:value, ...}) :1.0+Version:
Syntax Two
$.ajax("ajax_intro.txt", {success: function(response){usingAddof the AJAX requestChange the text of the DIV element:1.5of the AJAX request:
$.ajax(name:value, name:value, ...) $("div").html(response); });Test see‹/›
$.ajax("ajax_intro.txt", {success: function(response){usingAddof the AJAX requestChange the text of the DIV element:1.0Version:
$.ajax({ url: "ajax_intro.txt", success: function(response){ $("div").html(response); } });Test see‹/›
Request the page ajax_get.php and send some other data:
$.ajax({ url: "ajax_get.php", data: {fname:"Seagull", lname:"Anna"}, success: function(response){ $("div").html(response); } });Test see‹/›
Request the page ajax_get.php, send some other data, and display an alert status message:
$.ajax({ url: "ajax_get.php", data: {fname:"Seagull", lname:"Anna"}, success: function(response, status){ $("div").html(response); alert(status); } });Test see‹/›
Use the HTTP POST method to request the page: ajax_post.php
$.ajax({ url: "ajax_post.php", method: "POST", success: function(response){ $("div").html(response); } });Test see‹/›
Use the asynchronous setting to specify a synchronous request:
$.ajax({ url: "ajax_get.php", async: false, data: {fname:"Seagull", lname:"Anna"}, success: function(response){ $("div").html(response); } });Test see‹/›
Use the dataType setting to specify the data type of the request:
$.ajax({ url: ""/javascript/myscript.js", dataType: "script" });Test see‹/›
If an Ajax request encounters an error, display a notification:
$.ajax({ url: "wrong_file.html", success: function(response){ $("div").html(response); }, error: function(xhr){ $("div").html("Error occurred: "); + xhr.status + " " + xhr.statusText); } });Test see‹/›
Request a file and notify the user upon completion. If the request fails, display a notification:
let request = $.ajax({ url: "ajax_get.php", data: {fname:"Seagull", lname:"Anna"} }); request.done(function(msg){ $("div").html(msg); }); request.fail(function(xhr, textStatus){ $("div").html("Request failed:") + textStatus); });Test see‹/›
Specify one or more name:value pairs for the AJAX request parameter.
Possible names: the values in the following table:
Name | Value type | Description |
---|---|---|
async | Boolean | A boolean value indicating whether the request should be processed asynchronously. The default is true |
beforeSend(xhr) | Function | The function to be executed before sending the request |
cache | Boolean | A boolean value indicating whether the browser should cache the requested page. The default is true |
complete(xhr,status) | Function | The function to be executed after the request is completed (after both success and error functions) |
contentType | Boolean or string | The content type to be used when sending data to the server. The default value is: 'application' / x-www-form-urlencoded |
context | Plain object | Specifies the 'this' value for all AJAX-related callback functions |
data | PlainObject or String or Array | Specifies the data to be sent to the server |
dataFilter(data,type) | Function | The function used to process the original response data of XMLHttpRequest |
dataType | String | The expected data type of the server response |
error(xhr,status,error) | Function | The function to be executed when the request fails |
global | Boolean | A boolean value specifying whether the global AJAX event handler should be triggered by the request. The default is true |
ifModified | Boolean | A boolean value specifying whether the request is successful only if the response has changed since the last request. The default value is: false. |
jsonp | String or boolean | Overwrites the callback function in jsonp requests |
jsonpCallback | Function | Specifies the name of the callback function for jsonp requests |
method | String | Specifies the HTTP method to be used for the request (GET or POST). The default is GET |
password | String | Specifies the password to be used in HTTP access authentication requests |
processData | Boolean | A boolean value specifying whether the data accompanying the request should be converted to a query string. The default is true |
scriptCharset | String | Specify the character set for the request |
statusCode | Plain object | An object that will be called with the numeric HTTP code and function when the response has a corresponding code$.ajax({ statusCode:{ 404:function(){ alert('Page not found'); } } }) |
success(response,status,xhr) | Function | Function to be executed when the request is successful |
timeout | Number | Local timeout for the request (in milliseconds) |
traditional | Boolean | A boolean value that specifies whether to use the traditional parameter serialization style |
type | String | methodalias. If you are using1.9In jQuery versions prior to .0, the type attribute should be used |
url | String | Specify the URL to which the request is to be sent. The default is the current page |
username | String | Specify the username to be used in HTTP access authentication requests |
xhr | Function | Function used to create XMLHttpRequest objects |