This automatic essay grader is a simple example of an Ajax (XMLHttprequest) interaction from Paul Adams’ Ajax for Beginners tutorial on Webmonkey.
function grade(essay) {
// Mozilla version
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
// IE version
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
essay=encodeURIComponent(essay);
xhr.open("POST","grade.php");
xhr.setRequestHeader(
'Content-Type',
'application/x-www-form-urlencoded; charset=UTF-8');
xhr.send(essay);
xhr.onreadystatechange=function() {
if (xhr.readyState==4) {
grade = xhr.responseText;
alert ("Nice essay. Your grade is " + grade);
}
}
return false;
}
Browse Our Tutorials
Cheat Sheets
Color Charts
Cut & Paste Code