Skip to content

Commit

Permalink
Fix automatic parsing of JSON responses in $.get/post
Browse files Browse the repository at this point in the history
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
mislav committed Feb 12, 2014
1 parent b3a7a6f commit a3fc82e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ <h1>Zepto Ajax unit tests</h1>
$.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({
Expand Down Expand Up @@ -254,6 +278,14 @@ <h1>Zepto Ajax unit tests</h1>
}), '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')
Expand Down
9 changes: 8 additions & 1 deletion test/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down

0 comments on commit a3fc82e

Please sign in to comment.