Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A shorthand method to posting JSON data using jQuery
(function($) {
$.postJSON = function(url, data, success, dataType) {
if (typeof data != 'string') {
data = JSON.stringify(data);
}
$.ajax({
url : url,
type: "post",
data: data,
dataType: dataType || "json",
contentType: "application/json",
success: success
});
}
})(jQuery);
@padcom

This comment has been minimized.

Copy link
Owner Author

@padcom padcom commented Jan 3, 2012

A shorthand version of the http://api.jquery.com/jQuery.post/ function that instead of doing a form post does a JSON post with the proper content-type. See http://bit.ly/ykJb4k for more information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment