dongnaizao8039 2013-09-30 23:58
浏览 38
已采纳

即使我在POST上收到200响应,$ _POST数据仍为空?

I've got a PHP application that uses AngularJS for the client, FlightPHP for REST services, and PHP in the back end.

However, even though I'm issuing a POST with this code here:

$scope.registerUser = function() {
    $http({
        method: 'POST',
        url: '/user/register',
        data: $scope.user
    }).success(function(data) {
                if (data.result === undefined) {
                    setAjaxMessage(data, false);
                    return;
                }

                if (data.result === 0) {
                    setAjaxMessage(data.message, true);
                }
                else {
                    setAjaxMessage(data.message, false);
                }
            }).error(function(data) {
                setAjaxMessage(data, false);
            });
};

and I'm getting a successful POST message via Firefox with the Params -> Request payload tab set to this data:

{"displayname":"user1","email":"user1@gmail.com","password":"abc123"}

when I issue this server-side:

Flight::route('POST /user/register', function() {
    echo var_dump($_POST);
    return;

    // register the user
    require 'core/register.php';
});

I get this:

array (size=0)
  empty

What gives here? I've never had an issue with the $_POST data before. I have a feeling it has something to do with FlightPHP, but I can't put my finger on it. It's getting into the handler for the POST as you can see.

  • 写回答

2条回答 默认 最新

  • duanjuan1103 2013-10-01 00:18
    关注

    Alright, this one is a bit funky. But it is because of FlightPHP. Normally, the answer provided by Mike Brant would be 100% correct! However, FlightPHP reads that input before I can, and according to the PHP documentation it can only be read once.

    So, where does FlightPHP put it? Ha, now that's an odd one. They put it in a property named body, so I had to do this to grab those JSON values:

    $vars = json_decode(Flight::request()->body, true);
    

    The Flight::request()->body returns the JSON string. But then that needs decoded and turned into an associative array, so that's why I'm passing the true as the second parameter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?