Last active
February 20, 2023 17:42
-
-
Save jasonhinkle/3eb33a05c562643895d5 to your computer and use it in GitHub Desktop.
jQuery AJAX Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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