Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ajax
jquery
communitycreator

What is ajaxSetup() method in jQuery?

Shubham Singh Kshatriya

In jQuery, the ajaxSetup() method is used to set the default configuration for all future AJAX requests.

Syntax

$.ajaxSetup({
   key: value,
   key: value,
   ... 
});

Parameters

The parameters of ajaxSetup() are one or more key-value pairs. Some of the possible parameters are as follows:

  1. url: To specify the URL at which the request will be made.
  2. type: To specify the type of request. Possible values are GET, POST, PUT and DELETE.
  3. xhr: To create an instance of XMLHTTPRequest().
  4. contentType: To specify the content type when sending data to the server.
  5. dataType: The expected data type of the response.
  6. processData: A boolean to specify whether to process the data sent with the request into a query string.
  7. async: To specify whether the request should be made asynchronously or not.
  8. username: To specify the username that will be used in the HTTP Access Authentication request.
  9. password: To specify the password that will be used in the HTTP Access Authentication request.
  10. data: The data sent to the server.
  11. success: A function to execute when the request is successfully completed.
  12. error: A function to execute when the request fails.
  13. complete: A function to execute when the request gets completed.
  14. beforeSend: A function to execute before the request is made.
  15. dataFilter: A function to handle the raw response data of the XMLHttpRequest.
  16. traditional: A boolean to specify whether to use the traditional style of param serialization.
  17. timeout: To specify the local timeout (in milliseconds) of the request.
  18. context: To specify the this value for all AJAX callback functions.

Example

Console
Calling ajaxSetup()

We set an AJAX configuration using the ajaxSetup() method to fetch some product details. With this, every AJAX request will use this configuration only.

Upon the click of both buttons, we have added a listener that will initiate an AJAX request. Since we haven’t added any configurations inside the ajax() method, it will use the one set by the ajaxSetup() method.

RELATED TAGS

ajax
jquery
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya
RELATED COURSES

View all Courses

Keep Exploring