HTML and CSS Reference
In-Depth Information
Obviously, this method could be extended to properly encode arrays and other
kinds of data as well. Because the encodeURIComponent function isn't guaran-
teed to be available, feature detection is used to conditionally define the method.
12.6.2.1 Encoding Data in ajax.request
For post requests, data should be encoded and passed as an argument to the send
method. Let's start by writing a test that ensures data is encoded, as in Listing 12.58.
Listing 12.58 Asserting data sent to post
function setUp() {
this.tddjsUrlParams = tddjs.util.urlParams;
/* ... */
}
function tearDown() {
tddjs.util.urlParams = this.tddjsUrlParams;
/* ... */
}
TestCase("RequestTest", {
/* ... */
"test should encode data": function () {
tddjs.util.urlParams = stubFn();
var object = { field1: "13", field2: "Lots of data!" };
ajax.request("/url", { data: object, method: "POST" });
assertSame(object, tddjs.util.urlParams.args[0]);
}
});
Making this test pass isn't so hard, as Listing 12.59 shows.
Listing 12.59 Encoding data if any is available
function request(url, options) {
/* ... */
options = tddjs.extend({}, options);
options.data = tddjs.util.urlParams(options.data);
/* ... */
}
 
Search WWH ::




Custom Search