<<Back to Article

Ajax Essay Grader

/skill level/
/viewed/
0 Times

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;

}

  • This page was last modified 05:22, 15 May 2008.
Edit this article
Reddit Digg