Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Lack of "Intelligent Guess" of Content-Type in Ajax Response #693

Closed
joemccann opened this Issue · 6 comments

3 participants

@joemccann

By no means am I suggesting you should have an intelligent guess like jQuery, but while testing for deltas when falling back to jQuery, I noticed the following issue:

$.post('/signup', $signupForm.serialize(), function(resp){

  // This is a weird delta between zepto and jquery...
  var r = (typeof resp === 'string') ? JSON.parse(resp) : resp

  log(r)

})

Now, I dug into the jQuery source to see what they are doing and they are checking the content-type header in the response to "guess intelligently" as to the typeof response. I checked the zepto ajax code and wasn't sure the best way of patching it so I figured I'd rather at least mention the issue here to possibly start a discussion around whether this makes sense to add to zepto or not.

@mislav
Collaborator

I'm not sure which feature are you missing in Zepto? If dataType for the request was "json", or the response Content-Type is "application/json", Zepto does JSON.parse automatically.

It might be that your server is returning JSON responses with a type other than "application/json". In that case, use this form:

$.post('/signup', $signupForm.serialize(), function(resp){...}, "json")
@mislav mislav closed this
@joemccann

The type is certainly application/json. Bizarre.

@mislav
Collaborator
@kevinknelson

This is definitely a bug with $.post (not with $.ajax). The "relevant test" linked above only tests against the ajax() method directly which leaves the dataType as NULL and allows the mimeToDataType method to be called on this line:

      dataType = dataType || mimeToDataType(settings.mimeType || xhr.getResponseHeader('content-type'))

However, when using $.post() and not defining the 4th parameter, dataType gets defined as the success callback so "dataType || mimetoDataType" results in dataType again still being the callback and then the dataType == 'json' condition never gets met and the response remains a string.

This can be easily worked around, but, I think it is important to note that this is not how jQuery works. When the 4th parameter is missing in jQuery the success callback gets assigned to the dataType, but I guess they handle it differently when checking the dataType because calling the exact same service with the same parameters, jQuery will return an object while Zepto returns a string.

Anyway, hopefully that's enough information for this issue to be properly listed as a bug.

If posting this here doesn't get viewed and re-open the issue, I'll post it as a new issue...just didn't want to create something new if I didn't have to.

@mislav mislav referenced this issue from a commit
@mislav mislav 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
a3fc82e
@mislav
Collaborator

@joemccann Whoops, seems like you were right. Our tests didn't catch this particular usage, and as @kevinknelson points out, this was indeed broken. I just fixed this in master.

@mislav
Collaborator

Thanks @kevinknelson for the explanation of the bug :heart:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.