Skip to content

Instantly share code, notes, and snippets.

@marsyang1
Last active February 25, 2019 04:37
Show Gist options
  • Save marsyang1/bdbeba2461222d4cf408e4cb6415de8b to your computer and use it in GitHub Desktop.
Save marsyang1/bdbeba2461222d4cf408e4cb6415de8b to your computer and use it in GitHub Desktop.
JQuery Note

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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment