<h3>Practice Set, Week 10, Simple AJAX</h3>
Complete the JS code to make an AJAX call to the url 'http://courses.dce.harvard.edu/~cscie3/ajax.php' The repsonse will be the JSON string:<br/><pre><code>
{
"course":"CSCSI E3",
"school":"Harvard University Extension",
"term": "Spring 2015",
"skills": [
"programming",
"javascript",
"DOM",
"AJAX",
"jedi master"
]
}
</code></pre>
<p>Your task is to create the event listener that handles the response (readystatechange) for a successful request, converts the response to a JSON object, and outputs the 'school' property value using logMessage(). You may assume that the response will be JSON with the keys noted above (though the values could, of course, be different). </p>
<p>Output will appear below:</p>
<div id="output"></div>
// Create the XHR, intitalize the connection with open())
// and send the request. This part is done for you.
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://courses.dce.harvard.edu/~cscie3/ajax.php");
xhr.send();
// YOUR CODE HERE: Add a readystatechange listener function to respond to the HTTP response
xhr.addEventListener("readystatechange", digestData);
function digestData(e) {
if(this.readyState === 4 && this.status === 200) {
//var response = this.response;
//var parsed = JSON.parse(response).school;
logMessage(JSON.parse(this.response.school));
}
}
// Utility function for logging convenience
// Logs msg to the element with given id
// If id is undefined, logs to #output
function logMessage(msg, id) {
if (!id) {
id = "output";
}
document.getElementById(id).innerHTML += msg + "<br>";
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.