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

Requesting JSON content using jQuery

jQuery defines the variable $, which exposes methods for everything you want to do with the interface. (There are ways to rename that variable, say if you're working with another JavaScript environment that uses the same variable, but I don't recommend it). Among the methods $ exposes is the ajax method, which you use to make AJAX queries. Let's see how.

How to do it...

Here's a whole page that makes an AJAX request. The AJAX code is in bold:

<!doctype HTML> <html> <head> <script type="text/javascript" src="//code.jquery.com/jquery-1.11.2.min.js"></script> </head> <body> <p>Hello world</p> <p> <div id="debug"></div> </p> <p> <div id="json"></div> </p> <p> <div id="result"></div> </p> <p>Powered by <a href="http://www.aprs.fi">aprs.fi</a></p> ...

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