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 jQuery’s ajax() method.

In the past time, most of the developer was using jQuery due to its power and simplicity. It was easy to learn the syntax of jQuery’s ajax method as compare to JavaScript’s inbuilt method like XMLHttpRequest. The structure of XMLHttpRequest has difficulties 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:
$.ajax({ 
    name: value, 
    name: value, 
    ... 
});

Parameters: Following given a list of the possible parameters for the ajax() method
Type: It uses to specify the type of request.
URL: It uses to specify the URL to send the request.
Username: It is using to specify a username to be used in an HTTP access authentication request.

xhr: It is using for creating the XMLHttpRequest object.

async: Its default value is true. It indicates whether the request should handle asynchronously or not.
beforeSend(xhr): It is a function that runs before the request sent.
dataType: The data type expected of the server response.
error(xhr, status, error): It uses to run if the request fails.
global: Its default value is true. It uses to specify that trigger global AJAX event handles for the requestor not
jsonp: A string overriding the callback function in a JSONP request.
jsonpCallback: It uses 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): When the request is finish and function this is running.
contentType: Its default value is: “application/x-www-form-urlencoded” and it is used when data send to the server.
context: It is using to specify the “this” value for all AJAX related callback functions.
data: It is using to specify data to send to the server.
dataFilter(data, type): It is using to handle the raw response data of the XMLHttpRequest.
password: It is using to specify a password to which uses in an HTTP access authentication request.
processData: Its default value is true. It uses to specify whether or not data sent with the request should transform into a query string.
scriptCharset: It uses to specify the request charset.
success(result, status, xhr): When the request complete. then It is starting running.
timeout: It is the local timeout for the request. It measured in terms of milliseconds.
Traditional: Whether or not to use the traditional style of param serialization. It use to specify this.

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