Javascript 如果按下按钮,我可以通过ajax将JS数组发布到php吗?

Javascript 如果按下按钮,我可以通过ajax将JS数组发布到php吗?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我需要将valuesArray传递给php如果单击了按钮,当单击另一个按钮时,我想用php打印这个数组 home.js: button.onclick = function (){ valuesArray = ['a', 'b', 'c'] $.post('index.php', {data: valuesArray}); } <?php If(isset($_POST['anotherButton'])){ $getData = $_POST['da

我需要将
valuesArray
传递给php如果单击了
按钮
,当单击另一个按钮时,我想用php打印这个数组

home.js:

button.onclick = function (){

    valuesArray = ['a', 'b', 'c']

$.post('index.php', {data: valuesArray});
}
<?php
    If(isset($_POST['anotherButton'])){
        $getData = $_POST['data'];
        print_r($getData);
    } 
?>
index.php:

button.onclick = function (){

    valuesArray = ['a', 'b', 'c']

$.post('index.php', {data: valuesArray});
}
<?php
    If(isset($_POST['anotherButton'])){
        $getData = $_POST['data'];
        print_r($getData);
    } 
?>

如果按钮被点击,我会

未定义索引:数据

这意味着
数据
没有传递给PHP

$.post('index.php', {"data": valuesArray},function(return,result){});
请注意“数据”周围的双引号。
还可能需要对值进行json编码吗

$.post('index.php', valuesArray, function(result) {
    // ....
});

查看文档:

尝试使用不同的索引名(而不是数据)