The ajax() method is use for executing the asynchronous HTTP request. This is the most common method which eveyone uses in their codes. Because no one wants to write whole 10 to 15 lines of code everytime when he wants to send the request.

Thats why library came into role to prevent you to write those lengthy codes again and again.

Below is the normal syntax, just one argument which take object of key value pairs.

 $.ajax({
    key1: value1,
    key2: value2,
    ...
});

There are so many key value pairs. I am just using the common use case key value pairs. Below is the full common code sample which every developer use.

$.ajax({
    url: 'sample-url.php', // URL to send the request. By default is the current page.
    data: { username: 'root', passsword: 'root' }, // Data to be sent to the server.
    type: 'POST', // Type of request. (GET or POST)
    success: function(res) { // This method will execute when you get successful response.
        console.log(res);
    },
    error: function(err) { // This method will execute when your request fail.
        console.log(err);
    }
});

Last Modified: 2 years ago