Bug Tracker

Modify

Ticket #1900 (closed bug: fixed)

Opened 6 years ago

Last modified 15 months ago

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
Blocking: Blocked by:

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

comment:1 Changed 6 years ago by davidserduke

  • Status changed from new to closed
  • Resolution set to invalid
  • Milestone changed from 1.2.1 to 1.2.2

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 5 years ago by Encosia

  • Status changed from closed to reopened
  • Resolution invalid deleted

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

  • Cc Encosia, flesler added

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

comment:4 Changed 4 years ago by dmethvin

  • Status changed from reopened to closed
  • Resolution set to invalid

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

  • Status changed from closed to reopened
  • Resolution invalid deleted

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

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

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

  • Status changed from reopened to closed
  • Version changed from 1.2.1 to 1.4a1
  • Resolution set to fixed
  • Milestone changed from 1.2.2 to 1.4

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.