admin管理员组文章数量:1435859
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.
-
1
What exact output do you see for your
var_dump
? PS:var_dump
outputs by itself, you don't need additionalecho
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
2 Answers
Reset to default 7You 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.
本文标签: javascriptThe POST data is empty even though I39m getting a 200 response on the POSTStack Overflow
版权声明:本文标题:javascript - The $_POST data is empty even though I'm getting a 200 response on the POST? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745667976a2669397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论