Skip to main content

Bug Tracker

Side navigation

#14519 closed bug (notabug)

Opened November 06, 2013 01:48PM UTC

Closed November 08, 2013 02:00PM UTC

Last modified November 14, 2013 08:52AM UTC

jQuery Ajax converters behavior change

Reported by: seelamswathi@gmail.com Owned by:
Priority: high Milestone: 1.11/2.1
Component: ajax Version: 1.9.1
Keywords: Cc: jaubourg
Blocked by: Blocking:
Description

I have an Ajax request with dataType specified as json. When I get a response of type XML, I do an explicit conversion from xml to json in ajaxSetup-converters as shown in the jsfiddle here http://jsfiddle.net/sswathi/KkkVt/ . You can Check with jQuery versions 1.8.3 and 1.9+.

I tried upgrading to jQuery 1.9.1 from jQuery 1.8.2, I am getting 'parseerror', as it is expecting a json response. It is trying to parse my XML response as json as the conversion has not taken place.

The conversion worked fine till 1.8.2 but it no longer comes to the converter from version 1.9.1. I tried the same with the latest version 1.10.2 as well. But still getting the same error.

Is this the expected behavior? If so, how do I do the conversion as I am not sure what the response type would be but would prefer to have the data to be a json object.

Attachments (0)
Change History (6)

Changed November 06, 2013 07:06PM UTC by dmethvin comment:1

cc: → jaubourg
component: unfiledajax
milestone: None1.11/2.1
priority: undecidedhigh
status: newopen

This does look a bit suspicious .. jaubourg can you take a look?

Changed November 07, 2013 04:36AM UTC by csharbel@gmail.com comment:2

We have the same problem.

We have an XML response. We specified the dataType as json. It used to call the XML -> JSON convertor that we had added. But in 1.9.1 and later, we see that the convertor is not getting called. A parse error is also thrown as it tries to parse the XML (without conversion) as JSON.

Changed November 08, 2013 02:00PM UTC by jaubourg comment:3

resolution: → notabug
status: openclosed

The fact it used to call the XML to JSON converter was a bug.

Since you provide dataType: "json", ajax rightfully tries and parse the response as JSON. If you remove the dataType option, ajax will use the Content-Type header given by the server to determine which conversion to apply.

tl;dr: remove the dataType option and make sure the server sends a correct Content-Type.

Changed November 13, 2013 02:22PM UTC by dmethvin comment:4

Also, in the case where the server's Content-Type cannot be changed, just use dataType: text and parse the result explicitly in your callback. That makes it clearer you are working around a bug in the server, rather than hiding it in jQuery's magic.

Changed November 14, 2013 06:14AM UTC by csharbel@gmail.com comment:5

Agreed.

Suppose that the actual response from the server is XML (the server has also specified the content-type correctly). Now we want to convert it to JSON.

Now, you are asking us to leave the dataType empty. That does not work either. It does not call the XML to JSON convertor. Simply because it does not know what type it has to be converted to.

Where can we specify what the final target datatype needs to be. If you feel that dataType is where you specify the actual dataType, then what is the contentType attribute doing.

So earlier, it took the actual content-type from the server or from the ajax settings, looked at the dataType that was specified and went about choosing the convertor(s) to be used. In other words if you had specified the dataType as json earlier, it was working perfectly. Now you can get it work by specifying "xml json", but it causes existing code (of our customers) to break. Morover jquery's majic of automatically picking up the converters is lost.

This is what we feel. Correct us if we are wrong.

Replying to [comment:3 jaubourg]:

The fact it used to call the XML to JSON converter was a bug. Since you provide dataType: "json", ajax rightfully tries and parse the response as JSON. If you remove the dataType option, ajax will use the Content-Type header given by the server to determine which conversion to apply. tl;dr: remove the dataType option and make sure the server sends a correct Content-Type.

Changed November 14, 2013 08:52AM UTC by jaubourg comment:6

Replying to [comment:5 csharbel@…]:

Then use "* json", the actual content type as provided by the response Content-Type header will replace the * and everything should work as expected from there.