Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize data packet into query string for GET request #7

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/net.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"devDependencies": {
"almond": "^0.2.9",
"express": "^3.5.1",
"body-parser": "^1.9.0",
"formidable": "^1.0.14",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-connect": "~0.7.1",
Expand Down
22 changes: 19 additions & 3 deletions src/net/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function(Promise) {
throw new Error('This browser does not support XMLHttpRequest.');

}());

return new XMLHttpRequest(Array.prototype.slice.call(arguments, 0));
};

Expand Down Expand Up @@ -64,7 +64,7 @@ function(Promise) {
}

function handleReadyStateChange(promise, options) {

return function() {
if (this.readyState !== 4) {
return;
Expand All @@ -82,6 +82,17 @@ function(Promise) {

}

function serialize(options) {
var str = [];
for(var p in options) {
if (options.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(options[p]));
}
}

return '?' + str.join("&");
}

function request(optionsOrString, options) {

if (typeof optionsOrString === 'string') {
Expand All @@ -91,6 +102,10 @@ function(Promise) {
options = optionsOrString || {};
}

if ((options.method === METHODS.GET || options.method) && options.data) {
options.url = options.url + serialize(options.data);
}

var promise = new Promise();
promise.then(options.success || function(res) { return res; },
options.error || function(res) { return res; } );
Expand Down Expand Up @@ -150,7 +165,8 @@ function(Promise) {
post: post,
put: put,
delete: del,
request: request
request: request,
serialize: serialize
};

return api;
Expand Down
19 changes: 19 additions & 0 deletions test/net/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,22 @@ asyncTest("Adds headers to outgoing request", function() {
});
});

test("Can convert a JS object into a query string", function() {
var obj = {param1: 'one', param2: 'two'};

ok(ajax.serialize(obj) === '?param1=one&param2=two', 'turns object into query string');
});

asyncTest("Modifies URL for a GET request with data", function( assert ) {
ajax.get({
url: apiUrl('/net/ajax/with-query'),
data: {param1: 'paramOne'},

success: asyncHandler(function(req) {
ok(req.status === 200, 'returns a 200 status');
ok((req instanceof window.XMLHttpRequest), 'returns an XMLHttpRequest instance');
assert.equal(req.responseText, 'url was /net/ajax/with-query?param1=paramOne');
}),
error: noError()
});
});
12 changes: 9 additions & 3 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ var sys = require('sys');
var express = require('express');
var app = express();
var formidable = require('formidable');
app.use(express.json());
app.use(express.urlencoded());
var bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

/**
* net/ajax test endpoints
*/

app.get('/net/ajax', function(req, res, next) {

if (req.get("My-Custom-Header")) {
sys.inspect(req);
res.set("My-Custom-Header", req.get("My-Custom-Header"));
Expand All @@ -32,6 +33,11 @@ app.get('/net/ajax/redirect-destination', function(req, res, next) {
res.statusCode = 200;
res.end("Redirect Destination");
});
app.get('/net/ajax/with-query', function(req, res, next) {
res.statusCode = 200;
var param1 = req.params.param1;
res.end('url was ' + req.url);
});

/**
* net/json test endpoints
Expand Down