HTML and CSS Reference
In-Depth Information
12.6.2 Sending Data
In order for the POST request to make any sense, we need to send data with it.
To send data to the server the same way a browser posts a form we need to do
two things: encode the data using either encodeURI or encodeURIComponent
(depending on how we receive the data) and set the Content-Type header. We will
start with the data.
Before we head into the request test case to formulate a test that expects encoded
data, let's take a step back and consider what we are doing. Encoding strings isn't a
task unique to server requests; it could be useful in other cases as well. This insight
points in the direction of separating string encoding into its own interface. I won't
go through the steps required to build such an interface here; instead Listing 12.57
shows a very simple implementation.
Listing 12.57 Simplified url parameter encoder
(function () {
if (typeof encodeURIComponent == "undefined") {
function urlParams(object) {
if (!object) {
return "";
if (typeof object == "string") {
return encodeURI(object);
var pieces = [];
tddjs.each(object, function (prop, val) {
pieces.push(encodeURIComponent(prop) + "=" +
return pieces.join("&");
tddjs.namespace("util").urlParams = urlParams;
Search WWH ::

Custom Search