English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery ajax() Method

jQuery AJAX Methods

$ .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()

and several other more advanced alternatives, which are easier to use. However, if fewer common options are needed, $.ajax() can be used more flexibly.

Syntax::1.5+of the AJAX request:

Syntax One

$.ajax(url, {name:value, name:value, ...}) :1.0+Version:

Syntax Two

Example

$.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‹/›

Parameter value

Specify one or more name:value pairs for the AJAX request parameter.

Possible names: the values in the following table:

NameValue typeDescription
asyncBooleanA boolean value indicating whether the request should be processed asynchronously. The default is true
beforeSend(xhr)FunctionThe function to be executed before sending the request
cacheBooleanA boolean value indicating whether the browser should cache the requested page. The default is true
complete(xhr,status)FunctionThe function to be executed after the request is completed (after both success and error functions)
contentTypeBoolean or stringThe content type to be used when sending data to the server. The default value is: 'application' / x-www-form-urlencoded
contextPlain objectSpecifies the 'this' value for all AJAX-related callback functions
dataPlainObject or String or ArraySpecifies the data to be sent to the server
dataFilter(data,type)FunctionThe function used to process the original response data of XMLHttpRequest
dataTypeStringThe expected data type of the server response
error(xhr,status,error)FunctionThe function to be executed when the request fails
globalBooleanA boolean value specifying whether the global AJAX event handler should be triggered by the request. The default is true
ifModifiedBooleanA boolean value specifying whether the request is successful only if the response has changed since the last request. The default value is: false.
jsonpString or booleanOverwrites the callback function in jsonp requests
jsonpCallbackFunctionSpecifies the name of the callback function for jsonp requests
methodStringSpecifies the HTTP method to be used for the request (GET or POST). The default is GET
passwordStringSpecifies the password to be used in HTTP access authentication requests
processDataBooleanA boolean value specifying whether the data accompanying the request should be converted to a query string. The default is true
scriptCharsetStringSpecify the character set for the request
statusCodePlain objectAn 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)FunctionFunction to be executed when the request is successful
timeoutNumberLocal timeout for the request (in milliseconds)
traditionalBooleanA boolean value that specifies whether to use the traditional parameter serialization style
typeStringmethodalias. If you are using1.9In jQuery versions prior to .0, the type attribute should be used
urlStringSpecify the URL to which the request is to be sent. The default is the current page
usernameStringSpecify the username to be used in HTTP access authentication requests
xhrFunctionFunction used to create XMLHttpRequest objects

jQuery AJAX Methods