Side navigation
#1900 closed bug (fixed)
Opened November 07, 2007 08:19PM UTC
Closed December 05, 2009 02:21AM UTC
Last modified March 14, 2012 06:33AM UTC
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);
Attachments (0)
Change History (12)
Changed November 30, 2007 01:39AM UTC by comment:1
milestone: | 1.2.1 → 1.2.2 |
---|---|
resolution: | → invalid |
status: | new → closed |
Changed April 25, 2008 07:27PM UTC by comment:2
resolution: | invalid |
---|---|
status: | closed → reopened |
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:
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.
Changed August 08, 2008 03:51AM UTC by comment:3
cc: | → Encosia, flesler |
---|
I don't understand... the option 'dataType' should be 'json'.
Changed January 10, 2009 10:16PM UTC by comment:4
resolution: | → invalid |
---|---|
status: | reopened → closed |
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.
Changed March 05, 2009 04:02PM UTC by comment:5
resolution: | invalid |
---|---|
status: | closed → reopened |
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.
Changed March 05, 2009 04:10PM UTC by comment:6
Are you sure your rpcData variable in that call isn't undefined or something else that would evaluate to false?
Changed March 05, 2009 04:27PM UTC by comment:7
The data is a blank string, but it is defined.
Changed March 05, 2009 04:31PM UTC by comment:8
That's your problem. 0, NaN, '', false, null, and undefined all evaluate to false in a JavaScript conditional.
Changed March 05, 2009 04:44PM UTC by comment:9
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.
Changed March 05, 2009 04:45PM UTC by comment:10
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.
Changed May 20, 2009 04:20PM UTC by comment:11
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.
Changed December 05, 2009 02:21AM UTC by comment:12
milestone: | 1.2.2 → 1.4 |
---|---|
resolution: | → fixed |
status: | reopened → closed |
version: | 1.2.1 → 1.4a1 |
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.