how to pass array of objects as parameters on ajax post

时间:2016-04-04 16:52:01

标签: javascript jquery arrays params

I have the following array object

[{"site":"88333","event":"TEST","event_window":"4th April - 4th April","monitoring_start":"10-Apr-2016","monitoring_end":"10-Apr-2016"}]

I am trying to pass on ajax post as key value. I can have multiple rows. I have tried $.param(JSON.stringify(json_o)) and $.param(json_o), which does not work

Example shown here looks like array object which I have done but does not work.

var params = JSON.stringify(json_o);
        console.log(params);
        $.ajax({
            url : '../../api/netcool/add-event',
            data : params,
            type: 'POST',
            error : function (data, textStatus, jqXHR) {
                if(data.statusText != "abort")
                    console.log(data);
            },
            dataType : 'json',
            success : function (data) { 
                console.log(data);
            }
        });

1 个答案:

答案 0 :(得分:4)

Try this..

var params = [{"site":"88333","event":"TEST","event_window":"4th April - 4th April","monitoring_start":"10-Apr-2016","monitoring_end":"10-Apr-2016"}];
    console.log(params);
    $.ajax({
        url : '../../api/netcool/add-event',
        data : {params:params},
        type: 'POST',
        error : function (data, textStatus, jqXHR) {
            if(data.statusText != "abort")
                console.log(data);
        },
        success : function (data) { 
            console.log(data);
        }
    });

And in ../../api/netcool/add-event method.. you can acccess the parameter by

print_r($_REQUEST['params'][0]);
echo $_REQUEST['params'][0]['site'];