Bug Tracker

Ticket #1900 (reopened bug)

Opened 10 months ago

Last modified 1 month ago

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

Reported by: jweathers Assigned to: anonymous
Type: bug Priority: major
Milestone: 1.2.2 Component: ajax
Version: 1.2.1 Keywords: ajax, contentType
Cc: Encosia, flesler Needs: Review

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

Change History

Changed 9 months 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.

Changed 4 months ago by Encosia

  • status changed from closed to reopened
  • resolution 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.

Changed 1 month ago by flesler

  • cc set to Encosia, flesler

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

Note: See TracTickets for help on using tickets.