// minilab10-ex1.js
// Sample JavaScript solution for Mini-lab 10: AJAX
// (Example solution for Exercise 1.)
// INFO 343, Autumn 2012
// Morgan Doocy
// When the document is ready, attach a click handler to #fetch.
$(document).ready(function() {
$('#fetch').click(fetchClick);
});
// Make an Ajax request to fetch 'snippet.html'.
function fetchClick() {
$.ajax('snippet./*[<ins>]*/html/*[</ins>]*/', {
succes: injectSnippet,
error: ajaxError
});
}
// Inject the received data into #output.
function injectSnippet(data) {
/*[<ins>]*/$('#output').html(data);/*[</ins>]*/
}
// Display an alert with useful debugging information. (Called in case of an
// error in an Ajax request.)
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);
}