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 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率