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 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测