0

My problem is

jQuery.ajax({
    type: "POST",
    url: "../ajax/si-notificar_investigacion.php",
    data: {
        idincidente: $("#idincidente").val(),
        arrControlls : arrControlls
    },
    dataType: 'json',
    async: false,
    success: function(datos) {
        alert(datos);

    }
});

this is my ajax

Now I use arrControlls variable as array which is comes from another function

now if arrControlls is like

[0] = "test1";
[1] = "test1";
[2] = "test1";

then it is okey I get this variable as an array in action page

BUT if I use value like this

['sid_1'] = "test1";
['sid_2'] = "test1";
['sid_3'] = "test1";

then I do not get variable in action page WHY?

adding this lines for more detail

I am using jquery function for getting data

function getAllControllValue()
{

    var  arrControlls = new Array();
    $("#container-div input").each(function(){
        arrControlls[$(this).attr('id')] = $(this).val();
    });
    return arrControlls;
}
3
  • 2
    Arrays can't have keys, they are indexed with numbers only. Commented Feb 18, 2015 at 6:43
  • then what to do for keys ? Commented Feb 18, 2015 at 6:44
  • 2
    Use objects (which your array will be converted to anyway), but that probably means the structure of the data sent to the server changes as well Commented Feb 18, 2015 at 6:45

2 Answers 2

2

Arrays are expected to have sequential, numeric indexes. That is what they are for. You can give them named properties, but tools designed to do something with the data in the array tend to ignore them.

Given an array anywhere in data, jQuery will only pay attention to the numeric indexes.

If you want to have named keys, then use a plain object.

Initialise arrControlls with {} not new Array().

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct I just replace [] with {} and all the data are listed in action page thanks I mean new array
-1

User JSON.stringify like this: JSON.stringify(arrControlls)

jQuery.ajax({
    type: "POST",
    url: "../ajax/si-notificar_investigacion.php",
    data: {
        idincidente: $("#idincidente").val(),
        arrControlls : JSON.stringify(arrControlls)
    },
    dataType: 'json',
    async: false,
    success: function(datos) {
        alert(datos);

    }
});

4 Comments

What is the error you are getting? After doing this.
Not have any value in array in POST no error comming
JSON.stringify will also ignore named properties on arrays.
typeof Array !== type Object @pritzzz

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.