dongwei2030 2014-07-09 08:00
浏览 94
已采纳

我可以通过jquery.getJSON发送数组吗?

I have this jquery code:

$(".cashOutButton").click(function(e){
    e.preventDefault();
    var sendArray=[{'value': '123', 'type': 'Buy_Order_ID'},
                    {'value': '44.20', 'type': 'Set_Price'},
                    {'value': 'John', 'type': 'seller_Name'},
                    {'value': 'S', 'type': 'Sell_Type'}
                    ];

    var sendData = {'sendArray': sendArray};

    $.getJSON('addToSession.php',sendData,function(data){
        console.log(data);
    });        

})

and this php code - addToSession.php (it's just to test if it's working, the final code will be different):

<?php
print_r($_GET['sendArray']);
?>

It doesn't work. Nothing comes back. My question is, can I send an array of objects to $.getJSON? What other solutions are there to send arrays? It doesn't have to be array of objects, it could be array of arrays, if that would work.

  • 写回答

2条回答 默认 最新

  • douyou3619 2014-07-09 08:11
    关注

    You can, and this is returning the data you expect. You can test this by looking at the response in the Net tab of your browser's developer tools.

    It isn't being displayed in console.log because getJSON requires that the response it gets is JSON and you are responding something that claims to be an HTML document (the default for PHP) but which contains the output of print_r (which isn't valid HTML or JSON).

    Send JSON back and it will work:

    <?php
        header("Content-Type: application/json");
        echo json_encode($_GET['sendArray']);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog