Permalink
Browse files

Fix automatic parsing of JSON responses in `$.get/post`

Due to a bug in `parseArguments`, `dataType` would never be null, even
when not passed as an argument, and therefore content-type–based
processing would never kick in.

Fixes #693
  • Loading branch information...
1 parent b3a7a6f commit a3fc82e233b251e819a75b2b6f85d19abb3ccd2f @mislav mislav committed Feb 12, 2014
Showing with 46 additions and 6 deletions.
  1. +6 −5 src/ajax.js
  2. +32 −0 test/ajax.html
  3. +8 −1 test/server.coffee
View
@@ -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
}
}
View
@@ -218,6 +218,30 @@
$.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 @@
}), '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')
View
@@ -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'

0 comments on commit a3fc82e

Please sign in to comment.