如何使用数组通过ajax post发送数据

时间:2015-09-26 11:15:32

标签: javascript ajax

我有一个名为sendData的数组,它有索引sendData [0] [1] [2] .....

现在

sendData[0]="name="+variable+"&class="+variable;
sendData[1]="name="+variable+"&class="+variable;
sendData[2]="name="+variable+"&class="+variable;

现在我如何发送整个sendData?

2 个答案:

答案 0 :(得分:1)

如果允许jQuery,那么您可以尝试以下方法:

$.ajax({
        url:"Your URL", 
        type: "post",
        data: {'data' : JSON.stringify(sendData)},
        success: function (response) {
          console.log(response);
        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }
    });

答案 1 :(得分:0)

var sendData=new Object();
for(i=0;i<numberOfRows;i++){  //Number of rows
sendData[i]=new Object; 
for(var key in return_data_columns_detail[0]){    //contains the json data for column headers
var id="ip"+cnt+"addRow"+addIpRow;  //HTML ID for input fields contained inside table rows
var data=document.getElementById(id).value;
sendData[i][return_data_columns_detail[0][key]]=data;
cnt++;
}
addIpRow++
}


$.ajax({
    url:"../include/handle_event_add_team.php",
    type:"POST",
    data:sendData,
    dataType:"json",
    success:function(){
        console.log("after add button click team");
        ajax_post_display_table();
        //display_table_team();
    }
});

ok here is how i did it by creating an object which can me parsed into json data and sending to php server