我有一个表单,我使用angularJS http方法提交。
代码:
<script>
// define angular module/app
var formApp = angular.module('formApp', []);
// create angular controller and pass in $scope and $http
function formController($scope, $http) {
// create a blank object to hold our form information
// $scope will allow this to pass between controller and view
$scope.formData = {};
// process the form
$scope.processForm = function () {
$http({
method: 'POST',
url: 'http://localhost/angular/web/app_dev.php/testcall',
data: $.param($scope.formData), // 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
$scope.errorName = data.errors.name;
$scope.errorSuperhero = data.errors.superheroAlias;
} else {
// if successful, bind success message to message
$scope.message = data.message;
$scope.errorName = '';
$scope.errorSuperhero = '';
}
});
};
}
</script>
我的表格:
<div class="container">
<div class="col-md-6 col-md-offset-3">
<!-- PAGE TITLE -->
<div class="page-header">
<h1><span class="glyphicon glyphicon-tower"></span> Submitting Forms with Angular</h1>
</div>
<!-- SHOW ERROR/SUCCESS MESSAGES -->
<div id="messages" class="well" ng-show="message">{% verbatim %}{{ message}}{% endverbatim %}</div>
<!-- FORM -->
<form ng-submit="processForm()">
<!-- NAME -->
<div id="name-group" class="form-group" ng-class="{ 'has-error' : errorName }">
<label>Name</label>
<input type="text" name="name" class="form-control" placeholder="Bruce Wayne" ng-model="formData.name">
<span class="help-block" ng-show="errorName">{% verbatim %}{{ errorName}}{% endverbatim %}</span>
</div>
<!-- SUPERHERO NAME -->
<div id="superhero-group" class="form-group" ng-class="{ 'has-error' : errorSuperhero }">
<label>Superhero Alias</label>
<input type="text" name="superheroAlias" class="form-control" placeholder="Caped Crusader" ng-model="formData.superheroAlias">
<span class="help-block" ng-show="errorSuperhero">{% verbatim %}{{ errorSuperhero}}{% endverbatim %}</span>
</div>
<!-- SUBMIT BUTTON -->
<button type="submit" class="btn btn-success btn-lg btn-block">
<span class="glyphicon glyphicon-flash"></span> Submit!
</button>
</form>
<!-- SHOW DATA FROM INPUTS AS THEY ARE BEING TYPED -->
<pre>
{% verbatim %}{{ formData}}{% endverbatim %}
</pre>
</div>
</div>
我的功能,我想要来自http请求的表单数据:
/**
* @Route("/testcall")
* @Template()
*/
public function testAction() {
var_dump($_POST);
exit;
}
我唯一得到的就是这个网址:
http://localhost/angular/web/app_dev.php/test?name=Tommie&superheroAlias=Crawford
因此表单中的值保存在URL中。 但它不在$ _POST变量中.. 看起来函数testAction永远不会被访问
我该如何解决这个问题?
答案 0 :(得分:5)
您必须从请求中访问数据。如果你发布像
这样的JSON对象{
"foo": "bar"
}
你的控制器的动作应该是这样的:
public function postAction(Request $request)
{
$data = json_decode($request->getContent(), true);
$request->request->replace($data);
//echo below should output 'bar'
echo $request->request->get('foo');
//return response
//....
}
可以找到有关该信息的更多详细信息here