How to implement jQuery ajax() method? – Fetch API

As a JavaScript developer, you might be familiar with the Fetch API? If not check my article “How to Use the JavaScript Fetch API?”. Fetch API is used to make HTTP requests such as GET, POST, DELETE, etc. We can also make the HTTP request using the jQuery’s ajax() method.

In the past time, most of the developer was using jQuery due to it’s power and simplicity. It was easy to learn the syntax of jQuery’s ajax method as compare to JavaScript’s inbuilt method like XMLHttpRequest. Structure of XMLHttpRequest was complicated, difficult to learn and was not good looking. So developers prefered jQuery ajax() method over the JavaScript’s XMLHttpRequest.

Let’s move forward and see how it works. Syntax of the ajax method looks like:

Syntax:

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

Parameters: The list of the possible parameters for the ajax() method are given below:
type: It is used to specify the type of request.
url: It is used to specify the URL to send the request to.
username: It is used to specify a username to be used in an HTTP access authentication request.
xhr: It is used for creating the XMLHttpRequest object.
async: It’s default value is true. It indicates whether the request should be handled asynchronously or not.
beforeSend(xhr): It is a function that is to be run before the request is being sent.
dataType: The data type expected of the server response.
error(xhr, status, error): It is used to run if the request fails.
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.
jsonp: A string overriding the callback function in a JSONP request.
jsonpCallback: It is used to specify a name for the callback function in a JSONP request.
cache: It’s default value is true. It indicates whether the browser should cache the requested pages.
complete(xhr, status): It is a function that is to be run when the request is being finished.
contentType: It’s default value is: “application/x-www-form-urlencoded” and it is used when data send to the server.
context: It is used to specify the “this” value for all AJAX related callback functions.
data: It is used to specify data to be sent to the server.
dataFilter(data, type): It is used to handle the raw response data of the XMLHttpRequest.
password: It is used to specify a password to be used in an HTTP access authentication request.
processData: It’s default value is true. It is used to specify whether or not data sent with the request should be transformed into a query string.
scriptCharset: It is used to specify the charset for the request.
success(result, status, xhr): It is to be run when the request succeeds.
timeout: It is the local timeout for the request. It measured in terms of milliseconds.
traditional: It is used to specify whether or not to use the traditional style of param serialization.

Example:

$.ajax({
    url: "http://example.com/api/user",
    success: function(result) {
        console.log(result);
    },
    error: function(error) {
        console.log(error)
    }
});

 

This is how you can implement HTTP request in jQuery. By default, the request is GET request and you can specify the type if you want other requests such as POST, DELETE, etc. Post request would look something like:

Example:

$.ajax({
    url: "https://example.com/api/user",
    type: "POST",
    data: {
        firstName: "Tester",
        lastName: "One"
    },
    success: function(result) {
        console.log(result);
    },
    error: function(error) {
        console.log(error)
    }
});

 

In the above example, the parameter type is specified to describe the request type(GET, POST, DELETE, PUT, etc) and data is the key-value pairs we want to send to their server. Here the data will be sent as a form data and if you want to send JSON then you have to pass the parameter contentType with value application/json. So likewise you can implement DELETE, PUT, PATCH, etc.

Leave a Reply