Ajax call request in jquery

Making an ajax call using jquery is pretty simple, let’s create an ajax request that updates a specified contents every 20 seconds!

$.ajax({
url : 'YOUR URL WOULD GO HERE',
type : 'GET',
data : {
'parameter' : 10
},
dataType:'json',
success : function(data) {
console.log('Data: '+data);
},
error : function(request,error)
{
console.log("Request: "+JSON.stringify(request));
}
});

the above code is simply calling up a url, and passing up a data called parameter with a value of 10. which would be equivalent to the browser version -> (http://yoururl.com?parameter=10)

now, we have specified the data type (json), on success event and an on error event.

Let’s wrap it around a function called magicCall 😉 #swagAlert!

function magicCAll(x){

$.ajax({
url : 'YOUR URL WOULD GO HERE',
type : 'GET',
data : {
'parameter' : 10
},
dataType:'json',
success : function(data) {
$(x).text(data);
},
error : function(request,error)
{
$(x).text(error);
}
});

}

and to evoke it every 20 seconds:

window.setInterval(function(){
magicCAll("#wrap");
}, 20000);

That’s it 🙂

About the author

sarabpreet

Add comment

By sarabpreet

Latest from Sarabpreet