Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#14519 closed bug (notabug)

jQuery Ajax converters behavior change

Reported by: seelamswathi@… 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.

Change History (6)

comment:1 Changed 6 years ago by dmethvin

Cc: jaubourg added
Component: unfiledajax
Milestone: None1.11/2.1
Priority: undecidedhigh
Status: newopen

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

comment:2 Changed 6 years ago by csharbel@…

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.

comment:3 Changed 6 years ago by jaubourg

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.

comment:4 Changed 6 years ago by dmethvin

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.

comment:5 in reply to:  3 ; Changed 6 years ago by csharbel@…

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 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.

comment:6 in reply to:  5 Changed 6 years ago by jaubourg

Replying to 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.

Note: See TracTickets for help on using tickets.