AJAX - The Complete Reference

(avery) #1

170 Part I: Core Ideas


There are <strong>$votes</strong> total votes. The average is <strong>$average
</strong>. You can see the ratings in the <a href='http://ajaxref.com/ch4/
ratings.txt' target='_blank'>ratings file</a>";
$message = base64_encode($msg);

echo $message;

In transmission, you see your encoded text aiding in visual security.

Once you receive the response, you can prep it for insertion in the page, but first you
need to decode the base64 encoding:

if (xhr.readyState == 4 && xhr.status == 200)
{
var responseOutput = document.getElementById("responseOutput");
responseOutput.innerHTML = decode64(xhr.responseText);
}

To decode the base64 response, use a routine like the one shown here:

function decode64(inputStr)
{
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu-
vwxyz0123456789+/=";
var outputStr = "";
var i = 0;
inputStr = inputStr.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i<inputStr.length)
{
var dec1 = b64.indexOf(inputStr.charAt(i++));
var dec2 = b64.indexOf(inputStr.charAt(i++));
var dec3 = b64.indexOf(inputStr.charAt(i++));
var dec4 = b64.indexOf(inputStr.charAt(i++));
var byte1 = (dec1 << 2) | (dec2 >> 4);
var byte2 = ((dec2 & 15) << 4) | (dec3 >> 2);
var byte3 = ((dec3 & 3) << 6) | dec4;
outputStr += String.fromCharCode(byte1);
Free download pdf