Chapter 4: Data Formats 139
PART I
when using a comma as separator, how many pieces are there? As long as the content is
encoded to replace the commas with its equivalent hex value, it will be okay, as
demonstrated in the plain text version of the rating example:
payload = "rating=" + ratingVal + ",comment=" + comment.replace(/,/g, "%2C");
Once again, the Content-Type header must be set, this time to text/plain:
xhr.setRequestHeader("Content-Type", "text/plain");
The transmission of the plain text content is shown here.
On the server side, a simple decode must still be performed, based upon the comma
being used as a separator.
$payloadString = $GLOBALS['HTTP_RAW_POST_DATA'];
$payloadArray = array();
$tmpPayloadArray = explode(",", $payloadString);
for ($i=0;$i<count($tmpPayloadArray);$i++)
{
$index = strpos($tmpPayloadArray[$i], "=");
$name = substr($tmpPayloadArray[$i], 0, $index);
$value = substr($tmpPayloadArray[$i], $index+1);
$payloadArray[$name] = $value;
}
In this particular case, each item will contain the name of the field, an equal sign, and
the value. It is possible to infer the meaning of a data value by its position in the payload.
This would make the payload much smaller, but also less flexible in the event of future
changes. You can find this example at http://ajaxref.com/ch4/textrequest.html.