Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change $.ajax() default settings?

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.

like image 207
StuperUser Avatar asked Jul 27 '11 18:07

StuperUser


People also ask

What is default data type in Ajax?

This is an Ajax Event. When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases.

What is the use of Ajax () method?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

What is AJAX setup?

The ajaxSetup() method in jQuery is used to set the default values for future AJAX requests. Syntax: $.ajaxSetup( {name:value, name:value, ... } )

Why is global Ajax is used?

global: It's default value is true. It is used to specify whether or not to trigger global AJAX event handles for the request. ifModified: It's default value is false. It is used to specify whether a request is only successful if the response has changed since the last request.


2 Answers

Try using $.ajaxSetup()

$.ajaxSetup({
  async: false
});
like image 110
Rocket Hazmat Avatar answered Oct 12 '22 23:10

Rocket Hazmat


You want ajaxSetup

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

 });
 $.ajax({ data: myData });
like image 29
Joe Avatar answered Oct 12 '22 23:10

Joe