AJAX stands for Asynchronous JavaScript and XML and its used for to make calls to the server to fetch some data. In this article, we will learn how to implement a simple API call using AJAX technology.
How to use GET method using AJAX:GET is used to request data from a external server.
$.ajax({
type: 'GET',
dataType:"json",
url: 'GETURL',
headers:{
'Authorization' : 'your token',
},
success: function (data, status, xhr) {
console.log('data: ', data)
}
});
How to use POST method using AJAX: POST
requests are used to send data to the API server to create or update a resource.
$.ajax({
url: 'POSTURL',
type: 'POST',
dataType: 'json',
data: person,
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
Read About: How do I POST with jQuery/Ajax in Django?
Leave a comment