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.
Filed under: Uncategorized | 13 Comments
Tags: ajax, javascript, jquery, json, php, post, web
that was helpful, thank you 🙂
You’re welcome 🙂
Using Dojo and JSON to Build Ajax Applications
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
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
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
I use this:
I use this JSON with a jQuery POST:
http://www.x0.org.ua/blog/user/1/view/23
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!
`., I am really thankful to this topic because it really gives up to date information -*,
Los ejercicios con mancuernas son grandes, muy grandes.
Muy buena pagina. Estoy contento el haberla encontrado.
Es agradable leer algo intersante.
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.