file_get_contents('php://input'); with application/x-www-form-urlencoded;

前端 未结 3 2052
小鲜肉
小鲜肉 2020-12-20 15:51

I\'ve read a few questions about the subject here on but couldn\'t find the answer I\'m looking for. I\'m doing some $.post with jQuery to a PHP5.6 server.

$         


        
3条回答
  •  甜味超标
    2020-12-20 16:06

    In order to send raw json data, you have to stop jQuery from url-encoding it:

        data = {"a":"test", "b":{"c":123}};
    
        $.ajax({
            type: 'POST',
            url:  '...',
            data: JSON.stringify(data), // I encode it myself
            processData: false          // please, jQuery, don't bother
        });
    

    On the php side, just read php://input and json_decode it:

    $req = file_get_contents("php://input");
    $req = json_decode($req);
    

提交回复
热议问题