2

I am using Jquery 1.9.1. Having problem in sending ajax data which has array data in one of the key. This is my code:

var obj = {"param1": 1, "param2": 2, "param3": [1, 2]};
$.ajax({
    url : url,
    type: "POST",
    data: obj,
    success: function(){}
});

But when I checked headers in network panel of developer tool. It is displayed as this:

 "param1": 1,
 "param2": 2, 
 **"param3[]"**: [1]
 **"param3[]"**: [2]

Why it is adding [] to the param3 key? Anyone has any idea?

I have used $.ajax before, this the first time it is sending like this.

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
Rahul Sagore
  • 1,588
  • 2
  • 26
  • 47

2 Answers2

1

This is completely correct, this is how arrays are send by a request. It is the same as the html form:

<input name="param3[]" value=1>
<input name="param3[]" value=2>

param3[]: [1], param3[]: [2] is just another notation for the param3 array, in your PHP backend it will be a single variable $_POST['param3'] with the [1,2] as value. (assuming you use PHP, for other languages it will be an similar but perhaps different variable.)

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
1

The square bracket (i.e. []) added in jquery 1.4 to handle the multiple dimension array. If you want to remove this you should set the traditional option to true, as shown below:

$.ajaxSettings({traditional: true});

Following link explain the details. issue with brackets in jQuery Form Data when sending data as json

Community
  • 1
  • 1
Avinash Jain
  • 7,200
  • 2
  • 26
  • 40