How to Make an AJAX POST Request With jQuery

We have only used browser Web APIs to perform AJAX requests. We can also execute an AJAX request using a library like jQuery. POST requests in jQuery are executed using the post() function.

The new JavaScript Fetch API, there’s really no need to import a whole library like jQuery just for AJAX.

Now let’s make a POST request using jQuery instead:

$.post(“https://jsonplaceholder.typicode.com/posts”,
{
id: 1,
title: “What is AJAX”,
body: “AJAX stands for Asynchronous JavaScript…”
},
function(data, status) {
if(status === “success”) {
console.log(“Post successfully created!”)
}
},
“json”)

post() takes in four arguments: the URL, request body, callback function, and data format.

When the request is successfully completed, the callback function passed to post() will be invoked. This function takes in the response and status from the post request, both as arguments.

In the callback function, we check for the request status and log a message to the console.

Leave a comment

Your email address will not be published. Required fields are marked *