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

jQuery ajaxSetup() method

jQuery AJAX Methods

$ .ajaxSetup() method sets default values for future Ajax requests.

Unless overridden by individual calls, all subsequent Ajax calls using any method will use the new settings until the next call to $ .ajaxSetup().

Syntax:

$.ajaxSetup({name:value,  name:value,  ...})

Instance

Set the default URL for all AJAX requests:

$.ajaxSetup({
  url:  "ajax_post.php"
});
//Now, every time an Ajax request is made, the URL "  ajax_post.php" will be used automatically:
$.ajax({
  data:  {fname:"Seagull",  lname:"Anna"},
  method:  "POST",
  success: function(response){
    $("div").html(response);
  }
});
Test to see‹/›

Set default URL and success function for all AJAX requests:

$.ajaxSetup({
  url:  "ajax_data.txt",
  success: function(response){
    $("div").html(response);
  }
});
$("button").click(function(){
  $.ajax();
});
Test to see‹/›

If an Ajax request encounters an error, a notification will be displayed:

$.ajaxSetup({
  url:  "wrong_file.html",
  success: function(response){
    $("div").html(response);
  },
  error: function(xhr){
    $("div").html("An error occurred:  " + xhr.status + "  " + xhr.statusText);
  }
});
$("button").click(function(){
  $.ajax();
});
Test to see‹/›

Note:The settings specified here will affect the calls to$.ajaxor derived classes based on Ajax (such asof $.get())All calls. Since other callers (such as plugins) may expect to use normal default settings, this may lead to undesirable behavior. Therefore,It is strongly recommended not to use the $.ajaxSetup() method. Instead, set options explicitly in the call or define a simple plugin.

Parameter values

The parameters use one or more name:value pairs to specify the settings for the AJAX request.

Possible names: 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 the 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 to trigger the global AJAX event handler for the request. The default is true
ifModifiedBooleanA boolean value specifying that the request should only be successful 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 in jsonp requests
methodStringSpecifies the HTTP method to be used for the request (GET or POST). The default is GET
passwordStringSpecifies the password 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 of the request
statusCodePlain objectAn object that will be called with the numeric HTTP code and function when the response has the 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 of the request (in milliseconds)
traditionalBooleanA boolean value specifying whether to use traditional parameter serialization style
typeStringmethodalias. If you are using1.9In jQuery versions prior to .0, you should use type
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 to create XMLHttpRequest objects

jQuery AJAX Methods