jQuery Ajax HTTP call method
$(document).ready(function(){
const Url='https://jsonplaceholder.typicode.com/posts';
$('.btn').click(function(){
$.ajax({
url: Url,
type:"GET",
success: function(result){
console.log(result)
},
error:function(error){
console.log('Error')
}
})
})
})
Get HTTP call method
The $.get method is used to execute GET requests. It takes two parameters: the endpoint and a callback function.
const Url='https://jsonplaceholder.typicode.com/posts';
$('.btn').click(function(){
$.get(Url, function(data, status){
console.log(data);
});
})
Post HTTP call method
The $.post method is another way to post data to the server. It take three parameters: the url, the data you want to post, and a callback function.
const Url='https://jsonplaceholder.typicode.com/posts';
const data={
name:"Said",
id:23
}
$('.btn').click(function(){
$.post(Url,data, function(data, status){
console.log("Data: " + data.name + "\nStatus: " + status);
});
})
$.getJSON
The $.getJSON method only retrieves data that is in JSON format. It takes two parameters: the url and a callback function.
const Url='https://jsonplaceholder.typicode.com/posts';
$('.btn').click(function(){
$.getJSON(Url,function(result){
console.log(result);
});
})
jQuery methods combine together
const Url='https://jsonplaceholder.typicode.com/posts';
$('.btn').click(function(){
const data={ name:"Said",id:23};
$.ajax({
url: Url,
type: "GET", //or POST
// data:data, // if the type is POST
// dataType: JSON or HTML ,XML,TXT,Jsonp
success: function(result){
console.log(result)
},
error: function(error){
console.log("Error")
}
})
})