ajax post with json
$.ajax("api url",{
type: 'post',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({data1: 'data1', data2: 'data2'}),
}).done(function (result) {
alert(result.message);
doneFunction();
}).fail(function (xhr) {
console.log('xhr.responseText', xhr.responseText);
console.log('parseJSON(xhr.responseText).message', $.parseJSON(xhr.responseText).message);
// spring rest error vo message format
alert($.parseJSON(xhr.responseText).message);
});
ajax post from by json
with plugin https://github.com/macek/jquery-serialize-object
$.ajax("api url",{
type: 'post',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: $("#form").serializeJSON(),
}).done(function (result) {
alert(result.message);
doneFunction();
}).fail(function (xhr) {
console.log('xhr.responseText', xhr.responseText);
console.log('parseJSON(xhr.responseText).message', $.parseJSON(xhr.responseText).message);
// spring rest error vo message format
alert($.parseJSON(xhr.responseText).message);
});