Bug Tracker

Opened 15 years ago

Closed 13 years ago

Last modified 11 years ago

#1900 closed bug (fixed)

Setting contentType on $.ajax() call does not work.

Reported by: jweathers Owned by:
Priority: major Milestone: 1.4
Component: ajax Version: 1.4a1
Keywords: ajax, contentType Cc: Encosia, flesler
Blocked by: Blocking:


setting contentType did not have any affect in using the following code:

var o={
   type: "GET",
   dataType: "text",
   contentType: "application/json",
   url: getUrl("UIServices.asmx/RequestChangeAgent"),
   data: {PolicyNumber:"123123123",AgentNumber:"23e441234123",AgentComments:"ASDFASEASDFASDFASDF"},
   error: function(XMLHttpRequest, textStatus, errorThrown){
   success: function(msg){
     alert( "Data Saved: " + msg );

After debugging, it turned out that the check on s.data on line 2297 (in the ajax function) always returned false because s.data is always set null on line 2250 in cases using "get" method.

my fix was as follows:

//Old Code
//		if ( s.data )
//			xml.setRequestHeader("Content-Type", s.contentType);
//New Code
		if ( s.contentType )
			xml.setRequestHeader("Content-Type", s.contentType);

Change History (12)

comment:1 Changed 15 years ago by davidserduke

Resolution: invalid
Status: newclosed

I don't understand what you are trying to do. You are sending a "get" that has data but jQuery automatically sets data with a get and appends the data to the URL and sets the body data to null. And Content-Type tells what the format of the body data is, but we've just determined it was set to null so NO data was passed in the body.


So even if I made your proposed change, it wouldn't mean anything since no body data is there.

I'm going to close this bug as invalid for the moment. It's possible I'm missing something so if anyone thinks my reasoning is faulty please reopen the bug with your own reasoning and we can consider making the change.

comment:2 Changed 15 years ago by Encosia

Resolution: invalid
Status: closedreopened

It would be nice if this could be revisited, for those of us using jQuery with .NET.

One of the security features of .NET's JSON serializer is that it will only respond with JSON if the content-type of the request is "application/json; charset=utf-8". More on that at Scott Guthrie's blog:


What that means when calling ASP.NET services is that we have to jump through an extra hoop to get the content-type set correctly if we're making a call without data. For example:


Obviously, it’s something that can be worked around, but it would be more intuitive if setting the content-type always worked. I can’t think of what the drawback to allowing this would be.

comment:3 Changed 15 years ago by flesler

Cc: Encosia flesler added

I don't understand... the option 'dataType' should be 'json'.

comment:4 Changed 14 years ago by dmethvin

Resolution: invalid
Status: reopenedclosed

This page:



  • The request must be an HTTP POST request
  • The request’s content-type must be: “application/json; charset=utf-8″

If there is data, and a POST is used, the header for contentType will be set. The original reporter was trying to use a GET request, which seems to be excluded by ASP.NET anyway.

I'll close the ticket; reopen with more details if this seems wrong.

comment:7 Changed 14 years ago by alanwilliamson

Resolution: invalid
Status: closedreopened

Version: jQuery JavaScript Library v1.3.1

I am needing to set the contentType to "application/x-www-form-urlencoded" on a POST method, but JQuery is not working. It refuses to set it.

This code here does not work:

$.ajax( {
  url: "someendpoint.cfc?method=" + rpcMethod,
  data : rpcData,
  async: true,
  contentType: "application/x-www-form-urlencoded",
  type: "POST",
  dataType: "json",
  success : function(jsonResult){

*BUT* if i remove the #3999 line (if (s.data)) call and it just goes to:

xhr.setRequestHeader("Content-Type", s.contentType);

then it works beautifully.

comment:8 Changed 14 years ago by Encosia

Are you sure your rpcData variable in that call isn't undefined or something else that would evaluate to false?

comment:9 Changed 14 years ago by alanwilliamson

The data is a blank string, but it is defined.

comment:10 Changed 14 years ago by Encosia

That's your problem. 0, NaN, , false, null, and undefined all evaluate to false in a JavaScript conditional.

comment:11 Changed 14 years ago by alanwilliamson

well ... its "a" problem, not quite "who's" problem it is! :)

Then maybe that logic shouldn't be there ... not quite sure what that 'if' is trying to accomplish. Worse case, we set the ContentType all the time.

comment:12 Changed 14 years ago by Encosia

I definitely agree with that (See my posts earlier in this thread). I don't see what the problem would be with setting it regardless.

comment:17 Changed 14 years ago by gman

The trouble with a check to see if data is present before setting the contentType is that sometimes you don't want to post any data but still want to set a contentType. When calling an ASP.NET webservice with the aim of getting back some JSON for example. Surely if a contentType has been added to the dictionary it should be added to the request object whether any data is posted or not.

comment:20 Changed 13 years ago by john

Resolution: fixed
Status: reopenedclosed
Note: See TracTickets for help on using tickets.