views:

28

answers:

2

On server I am returning an array after some operations. I want to work with this array after successing of AJAX.

var addPaymentType = function(){
    var data = new Object()
    data["function"]        = "add";
    data["payment_type_id"] = $("#payment_types").val();
    data["data"]            = $("#saveform").serializeArray();
    $.ajax({
        type: "POST",
        url: location.href,
        data: data,
        dataType: "JSON",
        success : function (data)
        {
            console.debug(data['plan_pt_id']);
        }
    });
};

But data['plan_pt_id'] is undefined. If I return not an array, all works pretty. But how can I work with array?

Thank you.

+2  A: 

If data is an array, then you access it with e.g.

data[0]

If the first object in your array has a 'plan_pt_id' property, then you can access that with:

data[0].plan_pt_id

or with

data[0]['plan_pt_id']

The next object would be data[1], etc.

sje397
data[0] returns "{". Others return "undefined". My dataType is JSON. Maybe problem in it?
Alexander.Plutov
Try using lowercase 'json'.
sje397
data[0] returns 'undefined' instead of "{"(Pichalka
Alexander.Plutov
data['plan_pt_id'] works well. Thanks.
Alexander.Plutov
A: 

Lowercase "json" and data['plan_pt_id'],data['descr']

Alexander.Plutov
data['plan_pt_id'] won't work if data is an array. Perhaps you mean PHP associative array? In javascript, that's an object.
sje397
Yes. It is associative array. I didn't know that it is an object in JS.
Alexander.Plutov