What is the ajax() method in jQuery?

Overview

ajax() is a jQuery method that is used to perform an AJAX (asynchronous HTTP) request using jQuery.

The syntax for the ajax() method is as follows:

$.ajax({name:value, name:value, ... })

Parameters

The parameters specify one or more name/value pairs for the AJAX request:

  • async: A boolean value that indicates whether the request is asynchronous. The default value is true.
  • complete(xhr,status): The function to run when the request is finished, after the success and error functions.
  • dataType: The expected data type the server should respond with.
  • error(xhr,status,error): A function to run if the request fails.
  • success(result,status,xhr): A function to run if the request succeeds.
  • type: Specifies what type of request it is (GET or POST).
  • url: Most commonly used in this situation, it specifies the URL to send the request. The default is the current page.

To explore more parameters for the ajax() method, click here.

Example

Below is a code example that demonstrates the use of the ajax() method:

$.ajax({
url: "example.html",
success: function(result){
$("#div1").html(result);
}});

In the code above, a request is sent to the example.html and upon success, the result of that request is passed to a function with the success parameter.

Free Resources