Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#6811 closed bug (fixed)

$.ajax does not respect contentType if data is blank

Reported by: jloveridge Owned by:
Priority: undecided Milestone: 1.4.3
Component: ajax Version: 1.4.2
Keywords: Cc:
Blocked by: Blocking:


Assume func is a valid function and service is an object such as: {"url": "http://host.domain.com/serviceUrl", "type": "POST"}. The contentType is honored when data is not an empty object, {}, if the data object is empty then the contentType gets mangled.

$.ajax({"url": service.url, "type": service.type, "dataType": "json", "data": data, "success": func, "contentType": "application/x-www-form-urlencoded"});

Instead of the request being made with the contentType of "application/x-www-form-urlencoded", as is supposed to be the default anyway, the request gets made with the following values for the Content-Type header:

Firefox 3.6.6 : "text/plain"

Safari 5.0: "application/xml"

So far I have only tested this on the Mac.

In my case this is preventing successful requests to a Java application since the application refuses to accept Content-Types other than application/x-www-form-urlencoded. The result is that my requests are denied. Using Charles Proxy and modifying the Content-Type header does allow my request to succeed.

Change History (5)

comment:2 Changed 10 years ago by harbulot

This is a similar problem to ticket #6674. Would it be possible not to set the Content-Type header if there's no entity (unless it's explicitly specified perhaps)?

At the moment, it sends the Content-Type header if there's something in s.data, which may not be the entity (when s.data is passed in the query part).

Perhaps something like this:

if ( (s.data && type !== "GET" && type !== "HEAD" && type !== "DELETE") || type === "POST" || origSettings.contentType )

comment:3 Changed 10 years ago by dmethvin

need: CommitTest Case

Unfortunately, my patch above doesn't work! Please disregard.

I think at least part of the problem is that a check for a falsy s.data catches "" as well as null or undefined. This new patch passes the existing ajax unit tests but I wasn't able to reproduce the original bug on Windows. Can anyone test it on a Mac to see if it fixes the problem?


comment:6 Changed 10 years ago by john

Priority: undecided
Resolution: fixed
Status: newclosed

comment:7 Changed 10 years ago by addyosmani

#5943 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.