Ajax calling in different methods
Ajax (Asynchronous JavaScript and XML) is a technique for making asynchronous HTTP requests in web applications. You can use Ajax to call server-side methods or APIs from your web page without having to refresh the entire page
Using JavaScript
You can make Ajax calls using the XMLHttpRequest object in JavaScript.
Ajax post request
function post(data) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
}
};
xhr.open("POST", "https://api.example.com/submit", true);
xhr.setRequestHeader("content-type", "application/json");
xhr.send(JSON.stringify(data));
}
Using jQuery
function post (data) {
$.ajax({
url: "https://api.example.com/submit";
method: "POST",
contentType:"application/json" ,
data: JSON.stringify(data),
success: function (response) {
}
});
}