最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - The $_POST data is empty even though I'm getting a 200 response on the POST? - Stack Overflow

programmeradmin4浏览0评论

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":"[email protected]","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.

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":"[email protected]","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.

Share Improve this question asked Sep 30, 2013 at 23:58 Mike PerrenoudMike Perrenoud 68k32 gold badges166 silver badges238 bronze badges 7
  • 1 What exact output do you see for your var_dump? PS: var_dump outputs by itself, you don't need additional echo in front of it – zerkms Commented Oct 1, 2013 at 0:00
  • @zerkms, sorry I messed up the post while I was editing. Please see my edit. – Mike Perrenoud Commented Oct 1, 2013 at 0:01
  • What if you perform var_dump($_POST); in that framework's entry point as the first line? Before framework had a chance to drop it. – zerkms Commented Oct 1, 2013 at 0:04
  • @zerkms, exact same result. – Mike Perrenoud Commented Oct 1, 2013 at 0:05
  • 1 You wouldn't happen to be sending JSON instead of url encoded key value pairs? – Musa Commented Oct 1, 2013 at 0:06
 |  Show 2 more ments

2 Answers 2

Reset to default 7

You need to get your data from PHP's raw input if you are not posting a form-encoded query string (like is the case for raw JSON). Here is how you can read your JSON data into an appropriate PHP data structure:

$post_data = json_decode(file_get_contents('php://input'));

You should also explicitly set the Content-type header as follows:

$http({
    method: 'POST',
    url: '/user/register',
    data: $scope.user,
    headers: {'Content-Type': 'application/json'}
}).success(function(data) {
  // rest of your code

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.

发布评论

评论列表(0)

  1. 暂无评论