In this example, we will use a JSON object from a remote server file to update a web page when the user requests it.
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" }
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 calledholder
that will be updated with the JSON data.<a href="#">Click to make the JSON request!</a> <div id="holder"></div>
The final step is building our
Request.JSON
object (namedjsonRequest)
. In this object, we point our request to our JSON file using theurl
option. By using theonSuccess
method, we can run a function that sets#holder
div's HTML to the data inside the...