Brandon
Brandon

Reputation: 70012

Updating jQuery version has broken ajax load

I have an ASP.NET MVC application that uses jQuery 1.3.2.

On several of the pages there is a grid that lets you select multiple items, and then calls .load to load the details for the item.

this.$container.load(this.url, data, function() {
    // Show results
});

data containing an Array named s that contained the selected ids.

s = [ "2", "1" ]

Using jQuery 1.3.2, data would be posted as

s   2
s   1

Which was sent to

public ActionResult SomeAction(object[] s)

and everything worked great.

I tried to change the jQuery library to a more recent version (1.5.1) and now my controller doesn't receive the value of s because jQuery is posting data like so:

s[]   2
s[]   1

Do I need to assign an index in order for the controller to receive the value (s[1], s[2])? Do I need to change the controller action's signature or the way the array is generated? Right now the javascript to generate data is simply

var s = new Array();
for (var id in this._selected) {
  // Some checks
  s.push(id);
}

Upvotes: 2

Views: 120

Answers (1)

lonesomeday
lonesomeday

Reputation: 237995

If it worked in 1.3.2, you should be able to revert to the old behaviour by setting jQuery.ajaxSettings.traditional = true. This is a change in jQuery 1.4, and this setting allows backwards compatibility.

See the manual entry for $.param for more details.

Using your example data:

var data = {s: ["2", "1"]};

// jQuery 1.7
$.param(data); // "s%5B%5D=2&s%5B%5D=1"
jQuery.ajaxSettings.traditional = true;
$.param(data); // "s=2&s=1"

// jQuery 1.3.2
$.param(data); // "s=2&s=1"

Upvotes: 2

Related Questions