The Missing Parameter of jQuery.post

Posted on November 20, 2007
Filed Under Javascript | 8 Comments


When coding JavaScript that requires a lot of DOM manipulations, jQuery no doubt is my favorite JavaScript framework. Surprisingly, with file size of 26KB (packed distribution), it supports Ajax along with many other handy features.

If you've previously used jQuery's Ajax API, you probably noticed that there's a jQuery.getJSON() function call that grabs the server response from a given URL and parses it into a JSON object. However, you couldn't find the corresponding method from online documentation for post request, namely jQuery.postJSON.

I know that I can do "postJSON" with jQuery.ajax or simply use eval() function to parse the returned response, but I am not satisfied with either of them. As being fastidious, I want to have something that is as elegant as getJSON(). After spending some time on the source code, I finally found some workaround that I can live with. It turns out that the online documentation of jQuery is a little bit outdated because it shows jQuery.post only accept three parameters: jQuery.post( url, [data], [callback] , in fact jQuery.post accepts a fourth parameter which is the data type. That means I can do something like:

var callback = function (data, textStatus) {
  //data now will be a JSON object instead of plain text
};
jQuery.post( url, data, callback, "json") ;

Indeed, jQuery.get takes a fourth parameter as well:

get: function( url, data, callback, type ) {
   ...
}

and jQuery.getJSON is simply a jQuery.get with the fourth parameter as "json":

getJSON: function( url, data, callback ) {
  return jQuery.get(url, data, callback, "json");
}

Hopefully jQuery team can update their documentation soon, as it causes unnecessary confusions.

Go to Top

More interesting posts ...

Comments
8 Comments so far
  1. djot December 10, 2007 6:33 am

    This is what "jQuery" said to the postJSON idea long ago:
    http://dev.jquery.com/ticket/197

  2. Shu December 11, 2007 5:22 am

    Great!
    Thank you very much, I was looking for .postJSON since the first time I used jQuery. :)

  3. Vaviloff February 21, 2008 3:45 am

    Many thanks to this note, came in very handy at the first place when I typed "jquery post callback json" into Google.

  4. chief September 7, 2008 10:52 am

    Many, many thanks for this note. I have been pulling my hair out for about an hour trying to figure out why I couldn't get $.post to recognize my JSON result. I hope they update their documentation soon.

  5. Foo Bar January 1, 2009 11:06 am

    I couldn't get $.post to recognize my JSON result and so this note literally saved my life.

    Much thanks. :3

  6. Pul July 2, 2009 11:54 pm

    In fact, jQuery.get and jQuery.post are both (like getJSON) functions of 1 rule:
    return jQuery.ajax({ "POST"/"GET", ... });

  7. Matt November 16, 2009 2:04 pm

    Thank you for this post! It turns out that Internet Explorer (IE7) caches results obtained from GET but not POST. I couldn't figure out why it was not returning the right result. After making this change, everything works great.

  8. MDReniHinounk November 24, 2009 2:54 am

    Hello
    buy cheap levitra
    Levitra is nothing else than a PDE5 inhabitor.
    levitra pills
    At that time you should also know about that what amount of dose you need to take and after how much time.
    http://www.mwdcabinets.com/ - generic vardenafil
    Therefore you should not take much pain about it.

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments

Attention
If you want to post source code, please wrap it with <pre> and </pre>

Categories

Polls

  • Tell other about your screen resolution (pulled from web stats):

    View Results

    Loading ... Loading ...