处理PHP $ _POST与AngularJS? [英] Handling PHP $_POST with AngularJS?
问题描述
我想提出一个PHP后端的AngularJS应用程序,我在试图坚持一些数据写入到数据库中。我碰到一个障碍,我试图跳过。
当我做了 var_export($ _ POST)
得到了一个空白(空)数组作为回报。
然而,当我做了 $回报json_de code(的file_get_contents(PHP://输入),FALSE);
我得到一个完整的数组我期待值?
我如何使用$ _POST变量与下面的?
我AngularJS code:
$ scope.testPostRequest =功能(){
$ HTTP({
方法:POST,
网址:'requestHandler.php',
数据:JSON.stringify($ scope.scores),//传递数据串
标题:{
内容类型:应用程序/ x-WWW的形式urlen codeD
} //设置标题等等角度传递信息的表格数据(而不是请求负载)
})。成功(功能(数据){
的console.log(数据);
如果(!data.success){
//如果没有成功,绑定错误错误变量
}其他{
//如果成功,绑定成功消息消息
$ scope.message = data.message;
}
});
}
我的整个PHP code(我只是注释掉我感兴趣的回报率)。
// $数据= json_de code(的file_get_contents(PHP://输入),FALSE);
//返回var_export($的数据);
返回var_export($ _ POST);
ExpertSystem是正确的关于如何需要对数据进行格式化。
不理想,但jQuery的可能持有的解决方案。如果你有一个参考的jQuery您可以设置数据:
数据:$ .PARAM($ scope.scores);
从这里更多细节:
的http://jeecookbook.blogspot.com/2013/03/angularjs-trick-posting-data-using-url.html
从这篇文章摘自:<一href=\"http://stackoverflow.com/questions/11442632/how-can-i-make-angular-js-post-data-as-form-data-instead-of-a-request-payload\">How我可以让angular.js后的数据作为表格数据,而不是请求负载?
I am making an AngularJS application with a PHP backend, and am I attempting to persist some data to write into a database. I came across a hurdle I am attempting to jump over.
When I do a var_export($_POST)
I get a blank (empty) array as a return.
However, when I do a $return json_decode(file_get_contents("php://input"), false);
I get a full array with the values I expect?
How can I use the $_POST variable with the below?
My AngularJS code:
$scope.testPostRequest = function() {
$http({
method: 'POST',
url: 'requestHandler.php',
data: JSON.stringify($scope.scores), // pass in data as strings
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
} // set the headers so angular passing info as form data (not request payload)
}).success(function(data) {
console.log(data);
if (!data.success) {
// if not successful, bind errors to error variables
} else {
// if successful, bind success message to message
$scope.message = data.message;
}
});
}
My entire PHP code (I just comment out the return I am interested in).
//$data = json_decode(file_get_contents("php://input"), false);
//return var_export($data);
return var_export($_POST);
ExpertSystem is right about how the data needs to be formatted.
Not ideal, but jQuery may hold the solution. If you have a reference to jQuery you can set data to:
data: $.param($scope.scores);
More details from here: http://jeecookbook.blogspot.com/2013/03/angularjs-trick-posting-data-using-url.html
Taken from this post: How can I make angular.js post data as form data instead of a request payload?
这篇关于处理PHP $ _POST与AngularJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!