Bug Tracker

Ticket #11224 (closed bug: wontfix)

Opened 3 years ago

Last modified 2 years ago

$.ajaxSetup data object does not merge when $.ajax call passes a string.

Reported by: mike@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.1
Keywords: Cc:
Blocking: Blocked by:

Description

I'm trying to use $.ajaxSetup to add a default parameter to every ajax request on my page, but it's being made more difficult by this bug combined with some bad code a contractor wrote.

If I make any ajax calls using jquery, and I define my parameters as a urlencoded string instead of a javascript object, they will ignore the defaults set by $.ajaxSetup. consider the following example code:

 http://jsfiddle.net/HJFV3/2/

$(function(){

$.ajaxSetup({'data': {'secret': 'token'}}); $.get("/echo/json/", "param=stuff"); $.get("/echo/json/", {"param": "stuff"});

});

The first .get call requests /echo/json/?param=stuff whereas the second requests /echo/json/?param=stuff&secret=token.

Would be kind of nice if this would parse the query stringified version into an object, then do the merge.

Change History

comment:1 Changed 3 years ago by mike@…

I added the following hack to my local copy of jQuery. (started with version 1.4.2, line 4982.)

		// convert data if not already a string
		if ( s.data && s.processData && typeof s.data !== "string" ) {
			s.data = jQuery.param( s.data, s.traditional );
		}

		// HACK HACK HACK to support case where params were passed as a string.
		else if (origSettings.data && typeof(origSettings.data) === "string" && jQuery.ajaxSettings && jQuery.ajaxSettings.data){
			s.data += "&" + jQuery.param( jQuery.ajaxSettings.data, s.traditional );
		}

this works in my use case, but probably breaks others.

comment:2 Changed 2 years ago by dmethvin

  • Status changed from new to open

I am not sure whether this is a case we should handle, but if not we should document it.

comment:3 Changed 2 years ago by jaubourg

  • Status changed from open to closed
  • Resolution set to wontfix

We definitely shouldn't handle this. Cost is prohibitive. Don't encode your parameters OR deserialize them before giving them to $.ajax.

Note: See TracTickets for help on using tickets.