从angularJs post在控制器中持久化数据
我写了一些angularJs代码从angularJs post在控制器中持久化数据,angularjs,symfony,Angularjs,Symfony,我写了一些angularJs代码 var app = angular.module('registration', []); app.controller('registrationController', ['$scope', '$http',function($scope, $http){ $scope.userData = {}; var registrationUrl = Routing.generate('registrationAction'); console.log(regist
var app = angular.module('registration', []);
app.controller('registrationController', ['$scope', '$http',function($scope, $http){
$scope.userData = {};
var registrationUrl = Routing.generate('registrationAction');
console.log(registrationUrl);
$scope.sendForm = function() {
$http({
'url' : registrationUrl,
'data' : $.param($scope.userData),
'method' : 'POST',
'headers': {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data){
if(data.response == true) {
console.log('success');
$location.path(Routing.generate('indexAction'));
}
})
.error(function(error){
console.log('error');
});
}
}]);
在我的控制器中
public function registrationAction(Request $request)
{
if($request->isXmlHttpRequest() && $request->isMethod('POST')) {
$user = new Users();
$user->setEmail($request->request->get('email'));
$user->setPassword($request->request->get('password'));
$user->setUsername($request->request->get('login'));
$em = $this->getEntityManager();
$em->persist($user);
$em->flush();
return new JsonResponse(['response' => true]);
}
return new JsonResponse(['response' => false]);
}
结果,我的数据不会写入数据库,我的页面也不会重定向,但在我的控制台中,我并没有错误,状态代码为200。请帮助我因此,要将数据从angularJS客户端存储到symfony2服务器,您应该使用一个简单而轻松的请求侦听器来处理内容类型“application/json” 然后,在提交表单操作(客户端)中,只需执行以下操作:
$scope.sendForm = function(){
$http.post(registrationUrl, $scope.userData);
.success(function(data){
if(data.response == true) {
console.log(data.response);
$location.path(Routing.generate('indexAction'));
}
}).error(function(data){
console.log("data error ...");
});
};
存储操作(服务器)
因此,要将数据从angularJS客户端存储到symfony2服务器,您应该使用一个简单而轻松的请求侦听器来处理内容类型“application/json” 然后,在提交表单操作(客户端)中,只需执行以下操作:
$scope.sendForm = function(){
$http.post(registrationUrl, $scope.userData);
.success(function(data){
if(data.response == true) {
console.log(data.response);
$location.path(Routing.generate('indexAction'));
}
}).error(function(data){
console.log("data error ...");
});
};
存储操作(服务器)
$location.path
指示SPA之间的重定向,我认为您需要重定向到服务器..您可以使用window.location.href=Routing.generate('indexAction')
从控制台检查您的网络选项卡有什么问题?您的重定向是否不起作用?或者没有插入数据?@Ganesh Ghalame all不起作用,只需使用网络选项卡检查请求URI和参数是否正确。如果您想知道如何使用“网络”选项卡,可以使用此链接。还有一件事就是使用error\u报告(E\u ALL)启用php错误日志
$location.path
指示SPA之间的重定向,我认为您需要重定向到服务器..您可以使用window.location.href=Routing.generate('indexAction')
从控制台检查您的网络选项卡有什么问题?您的重定向是否不起作用?或者没有插入数据?@Ganesh Ghalame all不起作用,只需使用网络选项卡检查请求URI和参数是否正确。如果您想知道如何使用“网络”选项卡,可以使用此链接。还有一件事就是使用error\u报告(E\u ALL)启用php错误日志代码>我尝试你的解决方案。非常感谢。我试试你的解决办法。非常感谢。