jQuery is a library that fixes a lot of problems in older browsers. It also gives you a language for selecting elements in your document object model. It comes with a few functions for making AJAX calls, including one that is perfect for working with JSON data. We're starting off with the code from the movie on parsing JSON data with AJAX. Now, I'm running these files off the server, not a local hard drive. Whenever you're dealing with AJAX, you have to have some servers that are going to process the calls.
These files aren't on my machine; they're on an FTP server. We're going to need to add jQuery into our HTML file. We're going to go to the jQuery website, and I'm going to click on this Download jQuery link. I'll scroll down right here, and I'll copy this code to use the jQuery CDN. That's a content delivery network that hosts a lot of common scripts. I'm going to copy this one. I don't need this other one. That's just for compatibility with older versions of jQuery. I'll switch back to my code. I'll go into my index file, and I'm going to load this before I load myscript.js.
I'll put in a comment. Then I'm going to take all this code and paste it right there. I need to do another function, so this is where I make my AJAX call. The function is called getJSON, and what I need to do is pass it what I want to get, so in this case, it would be data.json, because this will be the JSON file in the same folder as this document, as well as a function literal, so no name. Into this function I'll receive the variable called info. When getJSON gets this data right here, it's going to feed it into info, and it's going to parse it so that it will be ready for me to use in my program.
- What is JSON?
- Creating simple data
- Communicating across sites with JSONP
- Rotating with jQuery Cycle