O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Parsing the returned JSON using jQuery

Finally, it's time to see how to get the returned JSON from the server and use it. You'll do this by registering an event handler on $.ajax to receive the resulting JavaScript object, which jQuery helpfully deserializes from JSON for you.

How to do it...

To get the result from the AJAX request, we need to add an event handler to the jQuery XMLHttpRequest object's done event, as follows:

function doAjax() { $('#debug').html("loaded... executing."); var request = { call: "kf6gpe-7" }; $.ajax({ type: "POST", url: "/", data: JSON.stringify(request), dataType:"json", }) .fail(function() { $('#debug').html( $('#debug').html() + "<br/>failed"); }) .always(function() { $('#debug').html( $('#debug').html() + "<br/>complete"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required