Bug Tracker

Opened 13 years ago

Closed 11 years ago

Last modified 8 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:

Description

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){
   	debugger;
   },
   success: function(msg){
   	 debugger;
     alert( "Data Saved: " + msg );
   }
 }
 $.ajax(o);

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 13 years ago by davidserduke

Milestone: 1.2.11.2.2
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.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17

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 12 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:

http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx

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:

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

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 12 years ago by flesler

Cc: Encosia flesler added

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

comment:4 Changed 12 years ago by dmethvin

Resolution: invalid
Status: reopenedclosed

This page:

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

says:

  • 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 11 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 11 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 11 years ago by alanwilliamson

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

comment:10 Changed 11 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 11 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 11 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 11 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 11 years ago by john

Milestone: 1.2.21.4
Resolution: fixed
Status: reopenedclosed
Version: 1.2.11.4a1
Note: See TracTickets for help on using tickets.