Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using jquery ajax with data that contains array, getting the parameter name with [] in the end

Tags:

jquery

$.ajax({
            url:"SomeURL",
            data: {                
                fields : ["field1", "field2"],                
            },
            success: function(data) {
            }
});

Getting this in the server:

name = fields[] , VALUE = field1
name = fields[] , VALUE = field2

(note the brackets)

Is that a bug? (This starting to happen after I upgraded to 1.5)

Thanx

like image 909
Rotem Avatar asked Feb 22 '11 16:02

Rotem


People also ask

Which method is used on the returned object of AJAX () method if the AJAX call fails?

If an AJAX request fails, you can react to the failure inside the callback function added via the fail() function of the object returned by the $. ajax() function. Here is a jQuery AJAX error handling example: var jqxhr = $.

What is the use of AJAX () method?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

What is the use of param () method in jQuery?

The param() method creates a serialized representation of an array or an object. The serialized values can be used in the URL query string when making an AJAX request.


1 Answers

It is not a bug, the serialization changed (but already in jQuery 1.4). Have a look at the traditional option:

traditional Boolean
Set this to true if you wish to use the traditional style of param serialization.

And from jQuery.param() (also have a look at the example):

As of jQuery 1.4, the $.param() method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting jQuery.ajaxSettings.traditional = true;.

like image 77
Felix Kling Avatar answered Sep 28 '22 09:09

Felix Kling