Skip to main content

Bug Tracker

Side navigation

#6811 closed bug (fixed)

Opened July 21, 2010 06:01AM UTC

Closed September 24, 2010 09:56PM UTC

Last modified October 24, 2010 04:18PM UTC

$.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:
Description

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.

Attachments (0)
Change History (5)

Changed July 25, 2010 05:30PM UTC by dmethvin comment:1

Changed July 29, 2010 11:08AM UTC by harbulot comment:2

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 )

Changed September 15, 2010 03:02AM UTC by dmethvin comment:3

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?

http://github.com/dmethvin/jquery/commit/dde3cdc384eb3dbc6382c360195d395fdff7d91a

Changed September 24, 2010 09:56PM UTC by john comment:4

priority: → undecided
resolution: → fixed
status: newclosed

Changed October 24, 2010 04:18PM UTC by addyosmani comment:5

#5943 is a duplicate of this ticket.