AJAX - The Complete Reference

(avery) #1

PART II


Chapter 5: Developing an Ajax Library 185


/* clear inProgress flag */
response.inProgress = false;

/* call either success or fail callback */
if (status == 200)
response.onSuccess(response);
else
response.onFail(response, status + " " + statusText);

/* clear out the response */
response = null;
}

That was a lot of code just to show the basic aspects of the library, but with that in hand,
take a look at what the simple rating example from Chapter 3 now looks like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Chapter 5 : XMLHttpRequest - Sending Data with GET Query Strings w/Library
</title>
<script src="http://ajaxref.com/ch5/ajaxtcr.js" type="text/javascript"></script>
<script type="text/javascript">

function rate(rating)
{
var url = "http://ajaxref.com/ch3/setrating.php";
var payload = "rating=" + AjaxTCR.data.encodeValue(rating);

var options = { method: "GET",
payload: payload,
successCallback : handleResponse
};

AjaxTCR.comm.sendRequest(url,options);
}

function handleResponse(response)
{
var responseOutput = document.getElementById("responseOutput");
responseOutput.innerHTML = response.xhr.responseText;
}

window.onload = function ()
{
var radios = document.getElementsByName("rating");
for (var i = 0; i < radios.length; i++)
{
radios[i].onclick = function (){rate(this.value);};
}
};
Free download pdf