Skip to content

Instantly share code, notes, and snippets.

@jasonhinkle
Last active February 20, 2023 17:42
Show Gist options
  • Save jasonhinkle/3eb33a05c562643895d5 to your computer and use it in GitHub Desktop.
Save jasonhinkle/3eb33a05c562643895d5 to your computer and use it in GitHub Desktop.
jQuery AJAX Snippet
$.ajax({
url: 'api/url',
method: 'POST',
data: {var:'val'},
// data: JSON.stringify({var:'val'}), // send data in the request body
// contentType: "application/json; charset=utf-8", // if sending in the request body
dataType: 'json'
}).done(function(data, textStatus, jqXHR) {
// because dataType is json 'data' is guaranteed to be an object
console.log('done');
}).fail(function(jqXHR, textStatus, errorThrown) {
// the response is not guaranteed to be json
if (jqXHR.responseJSON) {
// jqXHR.reseponseJSON is an object
console.log('failed with json data');
}
else {
// jqXHR.responseText is not JSON data
console.log('failed with unknown data');
}
}).always(function(dataOrjqXHR, textStatus, jqXHRorErrorThrown) {
console.log('always');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment