dongqucheng3851
2014-08-29 06:01 阅读 58

使用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 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']);
    
    点赞 评论 复制链接分享

相关推荐