12

How can I modify the default values of options for the $.ajax() function?

Ideally to do something similar to:

//set ajax async to false
$(someSelector).load(url, data, function(){});
//set ajax async to true

to allow me to carry out .post() synchronously.

gen_Eric
  • 214,658
  • 40
  • 293
  • 332
StuperUser
  • 10,181
  • 12
  • 75
  • 131
  • 2
    **Note to all:** Synchronous XMLHttpRequest on the main thread is deprecated. Do not use this approach. – Raptor Mar 06 '17 at 06:42

2 Answers2

26

You want ajaxSetup

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

 });
 $.ajax({ data: myData });
Joe
  • 77,580
  • 18
  • 124
  • 143
22

Try using $.ajaxSetup()

$.ajaxSetup({
  async: false
});
gen_Eric
  • 214,658
  • 40
  • 293
  • 332
  • 2
    Doing this gave me the following message: Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/. – patrick Jul 11 '15 at 09:41