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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64