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

Once the server returns the result, you need a way to get that result from the XMLHttpRequest object and convert the result from a string to a JavaScript object.

How to do it...

The XMLHttpRequest object defines the onreadystatechange attribute to which you assign a function that is called periodically throughout the lifespan of a request. Here's our doAjax function in its entirety, including a function assigned to this attribute to monitor the request for completion:

function doAjax() { var xmlhttp; xmlhttp = new XMLHttpRequest(); var request = { call: "kf6gpe-7" }; xmlhttp.addEventListener("loadstart", loadstart, false); xmlhttp.addEventListener("progress", progress, false); xmlhttp.addEventListener("load", load, false); ...

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