普通网友 2012-06-24 14:23
浏览 39
已采纳

如何使用jQuery的AJAX post request + php发送的数组?

I'm trying to pass an array in an ajax request, but apparently it doesn't work...

        $.post("process.php", array,
            function(data) {
                document.getElementById("output").innerHTML = JSON.parse(data);
            });

My question is, how to use the data sent in the process file?
The array is built like that: [key (0/1/2...)] => ["prod_id"]. The id varies.
I read somewhere using $_POST["key"]; would work, but it doesn't.

It'd be even better if I could just get the array as is in the process file.

process.php (really basic - just to check wether it's working or not.):

<?php print($_POST["test"]); ?>

  • 写回答

4条回答 默认 最新

  • duanhuhong5255 2012-06-24 14:48
    关注

    In order to receive data in php you need to send key/value pairs, however you are only sending a value.

    You receive in php with $_POST[key] which will return the value for that key.

    JS:

    $.post("process.php", {myAray: array}, function(data) {
                $("#output").html(data);
    });
    

    php

       $array= $_POST['myArray'];
    

    To return this array from php as text just to test your ajax can use var_dump( $_POST) or var_dump($array);

    If you intend to receive JSON in response from server, you do not need to use JSON.parse , jQuery will parse json internally. However you would need to add "json" as dataType argument to $.post

    $.post("process.php", {myAray: array}, function(data) {
                /* loop over json here*/
    },'json');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记