diff --git a/src/ajax.js b/src/ajax.js index e0f6eee99..1402c5af0 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -276,12 +276,13 @@ // handle optional data/success arguments function parseArguments(url, data, success, dataType) { - var hasData = !$.isFunction(data) + if ($.isFunction(data)) dataType = success, success = data, data = undefined + if (!$.isFunction(success)) dataType = success, success = undefined return { - url: url, - data: hasData ? data : undefined, - success: !hasData ? data : $.isFunction(success) ? success : undefined, - dataType: hasData ? dataType || success : success + url: url + , data: data + , success: success + , dataType: dataType } } diff --git a/test/ajax.html b/test/ajax.html index c262bb281..a7f18be57 100644 --- a/test/ajax.html +++ b/test/ajax.html @@ -218,6 +218,30 @@

Zepto Ajax unit tests

$.get('echo', { sample: 'plain' }, 'text') }, + testAjaxGetWithParamsAutoParsesJSON: function(t) { + t.pause() + $.get('json', { sample: 'data' }, t.reg.resumeHandler('success', function(data){ + t.assertEqual('data', data.query.sample) + t.assertEqual('world', data.hello) + })) + }, + + testAjaxGetNoParamsAutoParsesJSON: function(t) { + t.pause() + $.get('json', t.reg.resumeHandler('success', function(data){ + t.assertIdentical(0, $.map(data.query).length) + t.assertEqual('world', data.hello) + })) + }, + + testAjaxGetNullParamsAutoParsesJSON: function(t) { + t.pause() + $.get('json', null, t.reg.resumeHandler('success', function(data){ + t.assertIdentical(0, $.map(data.query).length) + t.assertEqual('world', data.hello) + })) + }, + testAjaxBeforeSendSetRequestHeader: function(t){ t.pause() $.ajax({ @@ -254,6 +278,14 @@

Zepto Ajax unit tests

}), 'text') }, + testAjaxPostAutoParsesJSON: function(t) { + t.pause() + var payload = { sample: 'data' } + $.post('create', payload, t.reg.resumeHandler('success', function(data){ + t.assertEqual('data', data.payload.sample) + })) + }, + testNumberOfActiveRequests: function(t) { var maxActive = 0, ajaxStarted = 0, ajaxEnded = 0, requestsCompleted = 0 t.assertIdentical(0, $.active, 'initial count mismatch') diff --git a/test/server.coffee b/test/server.coffee index 2fb2fd62b..1da075d5a 100644 --- a/test/server.coffee +++ b/test/server.coffee @@ -71,7 +71,8 @@ app.get '/test/jsonpBlah', (req, res) -> app.get '/test/json', (req, res) -> res.set 'Cache-Control', 'no-cache' - if /json/.test req.headers['accept'] + expectedType = req.headers['accept'] + if expectedType is '*/*' or /json/.test expectedType if req.query.invalid res.set 'Content-Type', 'application/json' res.send 'invalidJSON' @@ -82,6 +83,12 @@ app.get '/test/json', (req, res) -> else res.send 400, 'FAIL' +app.post '/test/create', (req, res) -> + res.json + action: 'created' + query: req.query + payload: req.body + app.all '/test/slow', (req, res) -> setTimeout -> res.send 'DONE'