【问题标题】:$.post - array from javascript to php$.post - 从 javascript 到 php 的数组
【发布时间】:2013-10-06 06:16:58
【问题描述】:

我在 javascript 中有这个数组:

arr = [1, "string", 3]

还有这个,我的 ajax 调用:

$.post("./ajax_book_client.php",
        {'client_info[]': arr},
    function(data) {
          // some stuffs here
        }
});

这是 php 的摘录:

<?php 
    $arr = $_POST['client_info'];

    // I want to access the array, indexed, like this:
    $arr[0] = 2;
    $arr[1] = "Hello";
    $arr[2] = 10000;

?>

但我收到此错误:

未捕获的类型错误:非法调用 jquery-1.8.3.min.js:2

这样做的正确方法是什么? 谢谢!

【问题讨论】:

    标签: javascript php jquery ajax


    【解决方案1】:

    }); 中删除} 以消除语法错误。

    也无需将[]client_info 一起使用,因此您可以将其删除。

    用途:

    <script>
    var arr = [1, "string", 3];
    $.post("./ajax_book_client.php",
            {'client_info': arr},
        function(data) {
              // some stuffs here
            }
    );
    </script>
    

    ajax_book_client.php

    <?php 
        $arr = $_POST['client_info'];
    
        echo $arr[0];
        echo $arr[1];
        echo $arr[2];
    ?>
    

    【讨论】:

      【解决方案2】:

      额外的大括号,改变:

      $.post("./ajax_book_client.php",
              {'client_info[]': arr},
          function(data) {
                // some stuffs here
              }
      });
      

      $.post("./ajax_book_client.php",
              {'client_info[]': arr},
          function(data) {
                // some stuffs here
             // } <--remove this
      });
      

      【讨论】:

        【解决方案3】:

        您的 JS 中有语法错误。删除一个}

        【讨论】:

          【解决方案4】:

          删除 client_info[]client_info 和额外的大括号

          试试

          <script>
          var arr = [1, "string", 3];
          $.post("./ajax_book_client.php",
              {'client_info': arr},
              function(data) {
                // some stuffs here
              }
          );
          </script>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-06-23
            • 1970-01-01
            • 1970-01-01
            • 2017-09-20
            • 2013-03-29
            • 2013-04-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多