Skip to content

Instantly share code, notes, and snippets.

@padcom
Created January 3, 2012 21:54
Show Gist options
  • Save padcom/1557142 to your computer and use it in GitHub Desktop.
Save padcom/1557142 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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