Published October 18th, 2009.
 * Hello Brandley, tonight I've tried to figure out how to do proper
 * JSON POSTs using "Content-type: application/json" and serialized JSON 
 * data in the content portion (body) of my HTTP requests. This is 
 * specified in the Twitter API for status updates and Identica JSON 
 * webservices (found via Mark Pilgrim). 
 * After some tests, I had to recognize that jQuery does not support JSON 
 * encoding in the core distribution and aside of $.getJSON(), there
 * is no $.postJSON().
 * Below is an proposed update. As it relies on your json plugin, I'd ask
 * you to add it to your code base so that other jQuery users can benefit
 * of it.

$.postJSON = function(url, data, callback) {
    return jQuery.ajax({
        'type': 'POST',
        'url': url,
        'contentType': 'application/json',
        'data': $.toJSON(data),
        'dataType': 'json',
        'success': callback

visit project

  • Samuel Tremblay

    Just do that:

    $.postJSON = function (url, data, callback) {
       $.post(url, data, callback, “json”);

  • Usman Jamil

    thanks for this! it is working fine for me!

  • Gilles Ruppert

    No, that won’t do the same. The “json” argument sets the Accept header, not the Content Type header. Also, by default, jQuery transforms JavaScript objects into application/x-www-form-urlencoded strings. If you want to send the actual stringified JSON, you need to do so yourself with either JSON.stringify() or – as the author uses here – with $.toJSON().

  • Raakie Sharma

    i tried this…it is successfully posting the content my php page..but i am not getting the success callback which is printed at php