// minilab12.js
// JavaScript solution file for Mini-lab 12: AJAX + XML
// INFO 343 Autumn 2012
// Morgan Doocy
// When the document's ready, attach a click handler to the 'fetch' button.
$(document).ready(function() {
$('#fetch').click(fetchClick);
});
// Handle clicking on the 'fetch' button. (Make Ajax request.)
function fetchClick() {
$.get('snippet.xml', injectBars);
}
function injectBars(xmldoc) {
var bars = xmldoc.getElementsByTagName('bar');
for (var i = 0; i < bars.length; i++) {
var contents = bars[i].firstChild.nodeValue;
$('<li>').text(contents).appendTo('#output');
}
}
// Provided Ajax error handler function (displays useful debugging information).
function ajaxError(jqxhr, type, error) {
var msg = "An Ajax error occurred!\n\n";
if (type == 'error') {
if (jqxhr.readyState == 0) {
// Request was never made - security block?
msg += "Looks like the browser security-blocked the request.";
} else {
// Probably an HTTP error.
msg += 'Error code: ' + jqxhr.status + "\n" +
'Error text: ' + error + "\n" +
'Full content of response: \n\n' + jqxhr.responseText;
}
} else {
msg += 'Error type: ' + type;
if (error != "") {
msg += "\nError text: " + error;
}
}
alert(msg);
}