dongqucheng3851 2014-08-29 06:01
浏览 91
已采纳

使用PHP从JSON中检索对象

I need to send some data stored in IndexedDB to server for some back-end manipulation. The needed data is fetched to a variable payLoad in javascript using JSON.stringify().

 payLoad = "[{"synch":0,"id":-1,"name":"Tester","email":"test@example.com","created":"2014-08-20T07:56:44.201Z"}]";
    $.ajax({
                type: "POST",
                url: "process.php",
                data: payLoad,      // NOTE CHANGE HERE
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    alert(msg);
                },
                error: function(msg) {
                alert('error');
                }

            });

Can I parse this JSON data to a PHP class?

  • 写回答

1条回答 默认 最新

  • douhuiqi3855 2014-08-29 06:07
    关注

    This way, you're just sending JSON raw in the body. Try this:

    $data = json_decode(file_get_contents('php://input'));
    

    If, on the other hand, you send data with this:

    data: { data: payLoad },
    

    Then you can simply do

    $data = json_decode($_POST['data']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100