JQUERY AJAX different ways
Below are different ways to perform AJAX Get and Post requests through jQuery.
GET request method 1
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
GET request method 2
$.get( "ajax/test.html", function( data ) {
$( ".result" ).html( data );
alert( "Load was performed." );
});
GET request method 3 – jqXHR
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.get( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});
POST request method 1
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
POST request method 2
$.post( "ajax/test.html", function( data ) {
$( ".result" ).html( data );
});
POST request method 3 – jqXHR
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});