Created
January 3, 2012 21:54
-
-
Save padcom/1557142 to your computer and use it in GitHub Desktop.
A shorthand method to posting JSON data using jQuery
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
(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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