Parsing JSON with a jQuery ajax object using POST

26Dec07

It’s a bit of a head-scratcher at first, seeing as the jQuery docs mysteriously disavow a postJSON possibility, let alone a function, but it’s definitely doable. So here’s the glorious code:

function postjson(url,data,bS,s,e){
$.ajax({type:”POST”,url:url,data:data,dataType:”json”,
beforeSend:bS,success:s,error:e});}

Use it like so:

postjson(“name.php”,”name=Bob”,
function(){$(“#name”).text(“…”);},
function(json){$(“#name”).text(json.name);},
function(){$(“#name”).text(“Error”);});

If name.php’s output looks like this…

{“name”:”Bob”}

…the above should, upon execution, change the ID “name” to contain the text “Bob.” Should the name variable come up missing, the ajax object will alert you accordingly.

About these ads


13 Responses to “Parsing JSON with a jQuery ajax object using POST”

  1. 1 Leander

    that was helpful, thank you :)

  2. 2 modulatum

    You’re welcome :)

  3. 3 Test

    Using Dojo and JSON to Build Ajax Applications

  4. 4 wil

    Can be done with

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

    See

    http://thecodecentral.com/2007/11/20/the-missing-parameter-of-jquerypost

  5. Nice article.

    You can check a similar simple example for using Jquery/JSON for autopopulate select at http://www.tech-freaks.in/Java-Programming/Javascripts/jquery-json.html

  6. 6 PiterJankovich

    My name is Piter Jankovich. oOnly want to tell, that your blog is really cool
    And want to ask you: is this blog your hobby?
    P.S. Sorry for my bad english

  7. 7 dima

    I use this:

  8. 8 dima

    I use this JSON with a jQuery POST:

    http://www.x0.org.ua/blog/user/1/view/23

  9. I’m new here!
    I like your blogpost. This is very interesting to read.
    I wonder why I didn’t find this blog before.

    I will surely share. Many of my Facebook friends will appreciate it.

    Gracias and keep’em coming!

  10. `., I am really thankful to this topic because it really gives up to date information -*,

  11. Los ejercicios con mancuernas son grandes, muy grandes.

  12. Muy buena pagina. Estoy contento el haberla encontrado.

    Es agradable leer algo intersante.

  13. Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically tweet my
    newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: