how to pass array of objects as parameters on ajax post

不羁岁月 提交于 2020-01-17 04:58:10

问题


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:


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'];


来源:https://stackoverflow.com/questions/36408465/how-to-pass-array-of-objects-as-parameters-on-ajax-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!