jquery ajax syntax code example
Example 1: ajax exmaple\
$.ajax({
url:'your url',
type: 'POST',
data: { myData: 'This is my data.' },
success: function (data, status, xhr) {
$('p').append('status: ' + status + ', data: ' + data);
},
error: function (jqXhr, textStatus, errorMessage) {
$('p').append('Error' + errorMessage);
}
});
Example 2: ajax call
$.ajax({
url: "Url",
dataType: "json",
type: "Post",
async: true,
data: {"Key":value,"Key2":value2},
success: function (data) {
},
error: function (xhr, exception, thrownError) {
var msg = "";
if (xhr.status === 0) {
msg = "Not connect.\n Verify Network.";
} else if (xhr.status == 404) {
msg = "Requested page not found. [404]";
} else if (xhr.status == 500) {
msg = "Internal Server Error [500].";
} else if (exception === "parsererror") {
msg = "Requested JSON parse failed.";
} else if (exception === "timeout") {
msg = "Time out error.";
} else if (exception === "abort") {
msg = "Ajax request aborted.";
} else {
msg = "Error:" + xhr.status + " " + xhr.responseText;
}
if (callbackError) {
callbackError(msg);
}
}
});
Example 3: jquery ajax syntax
$.ajax({
url: "post.php",
data: {
id: 123
},
type: "GET",
dataType : "json",
})
.done(function( json ) {
$( "<h1>" ).text( json.title ).appendTo( "body" );
$( "<div class=\"content\">").html( json.html ).appendTo( "body" );
})
.fail(function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
})
.always(function( xhr, status ) {
alert( "The request is complete!" );
});