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, the following sets a default for the url parameter before pinging the server repeatedly:

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

Now each time an Ajax request is made, the "ping.php" URL will be used automatically:

$.ajax({
  // url not set here; uses ping.php
  data: {'name': 'Dan'}
});

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 options 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 });

Support and Contributions

Need help with jQuery.ajaxSetup() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to jQuery.ajaxSetup()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • mizar

    In my application i noticed that the command :

    $.ajaxSetup({async:false});

    Is applied only to the next call, but another call would have it reset to async to true. I experienced this with different ajax call that i have to complete before to proceed…

    The workaround is to call $.ajaxSetup({async:false}); at every ajax call you need to be synchronous.

  • my live in JQUERY,JsOn

    its best from a simple ajax in JaVa/sCrIPT

  • http://twitter.com/stefanooldeman Stefano

    Why is context property not allowed in this method. Any ideas on this?

    • Guest

      I assume because the context is, by definition, global?

  • 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.

  • Liu_sl2005

    success handler can get data,and handle it
    but,when i use complete handler, i can not get data,
    why?

  • Nvcgame

    fg sdfg

  • Claude

    http://api.jquery.com/jQuery.a…/ settings.timeout says “See $.ajaxSetup() for global timeouts.”
    Where is the information on global timeouts?

  • http://phoscur.myopenid.com/ Phoscur

    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.

    • Johnwc723

      so you think its better to explicitly set this every single time you call jquery.load (and do it inconsistently probably)? this is great because I want to consistently handle 500 errors!

  • http://phoscur.myopenid.com/ Phoscur

    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.

  • Pashgel

    is .ajaxSetup is global
    i want make .ajax in function
    .ajaxSetup details work in .ajax in function ?