Book Image

MooTools 1.2 Beginner's Guide

Book Image

MooTools 1.2 Beginner's Guide

Overview of this book

MooTools is a simple-to-use JavaScript library, ideal for people with basic JavaScript skills who want to elevate their web applications to a superior level. If you're a newcomer to MooTools looking to build dynamic, rich, and user-interactive web site applications this beginner's guide with its easy-to-follow step-by-step instructions is all you need to rapidly get to grips with MooTools.
Table of Contents (14 chapters)
MooTools 1.2 Beginner's Guide
Credits
About the Authors
About the Reviewer
Preface

Time for action—working with Ajax and JSON


In this example, we will use a JSON object from a remote server file to update a web page when the user requests it.

  1. First, we need some sample JSON data to work with. In the following code block you can see an example of a JSON object that you can use as your sample data. Save this file as myjsondata.json.

    {
    "name": "Jacob Gube",
    "age": 26,
    "hairStyle": "Short",
    "location": "Bloomington"
    }
    
  2. Next, create an HTML document that will serve as the web page requesting JSON data. This document contains the<a> element that triggers the request, and a div called holder that will be updated with the JSON data.

    <a href="#">Click to make the JSON request!</a>
    <div id="holder"></div>
    
  3. The final step is building our Request.JSON object (named jsonRequest). In this object, we point our request to our JSON file using the url option. By using the onSuccess method, we can run a function that sets #holder div's HTML to the data inside the...