post json ajax code example
Example 1: jquery ajax post example
var formData = {name:"John", surname:"Doe", age:"31"};
$.ajax({
url : "https://example.com/rest/getData",
type: "POST",
data : formData,
async : false,
success: function(response, textStatus, jqXHR) {
console.log(response);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
Example 2: send json post ajax javascript
$.ajax({
url: 'users.php',
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify( { "first-name": $('#first-name').val(), "last-name": $('#last-name').val() } ),
processData: false,
success: function( data, textStatus, jQxhr ){
$('#response pre').html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
Example 3: jquery ajax post
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
Example 4: jquery $.post
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
jqxhr.always(function() {
alert( "second finished" );
});