jQuery API

jQuery.ajaxSetup()

jQuery.ajaxSetup( options )

Description: Set default values for future Ajax requests.

  • version added: 1.1jQuery.ajaxSetup( options )

    optionsA set of key/value pairs that configure the default Ajax request. All options are optional.

For details on the settings available for $.ajaxSetup(), see $.ajax().

All subsequent Ajax calls using any function will use the new settings, unless overridden by the individual calls, until the next invocation of $.ajaxSetup().

For example, we could set a default for the URL parameter before pinging the server repeatedly:

$.ajaxSetup({
  url: 'ping.php',
});

Now each time an Ajax request is made, this URL will be used automatically:

$.ajax({
  data: {'name': 'Tim'},
});

Note: Global callback functions should be set with their respective global Ajax event handler methods-.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()-rather than within the settings object for $.ajaxSetup().

Example:

Sets the defaults for Ajax requests to the url "/xmlhttp/", disables global handlers and uses POST instead of GET. The following Ajax requests then sends some data without having to set anything else.

$.ajaxSetup({
   url: "/xmlhttp/",
   global: false,
   type: "POST"

 });
 $.ajax({ data: myData });

User Contributions

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for jQuery.ajaxSetup() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • In my opinion, this function should not be used, in fact not even exist. It's manipulating the global jQuery object. Manipulating it can easily cause a lot of confusion in bigger projects.
  • Claude
    http://api.jquery.com/jQuery.a.../ settings.timeout says "See $.ajaxSetup() for global timeouts."
    Where is the information on global timeouts?
  • Liu_sl2005
    success handler can get data,and handle it
    but,when i use complete handler, i can not get data,
    why?
  • Baypooh7
    what is the default value for cache?

    true? false?
  • Baypooh7
    ^^ it is true, hu~ i need to change it at all
  • Ashankabiswas
    why do we make global=false in $.ajaxSetup
  • Miles
    To prevent global event handlers from being used, like the listeners error, success and complete.
  • Why is context property not allowed in this method. Any ideas on this?