我试图通过Angularjs向Symfony发送带有用户名和密码的ajax请求,但我无法访问帖子变量。这是我的代码:
HTML:
help(unittest.skip)
AngularController
<div>
<input type="text"
class="form-control defaultPassword"
ng-model="username"
placeholder="username">
</div>
<div class="form-group">
<input type="password"
ng-model="password"
class="form-control {% verbatim %}{{ passwordBox }}{% endverbatim %}"
placeholder="password">
</div>
<div>
<span>{% verbatim %}{{ message }}{% endverbatim %}</span>
<button id="submitBtn" class="btn" ng-click="login()">login</button>
</div>
});
这是symfony控制器:
todoApp.controller("LoginController",
function LoginController($scope, $http){
$scope.username = "";
$scope.password = "";
$scope.login = Login;
function Login(){
$http({
method: 'POST',
url: '/api/login',
data: {
username: $scope.username,
password: $scope.password
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
alert("error");
});;
}
这种检索数据的方法不起作用。我可以看到用户名和密码正在发送但我无法访问它们。你能帮我找到另一种方法让他们进入symfony控制器吗?
答案 0 :(得分:0)
<强> 角 强>
$http({
method : 'POST',
url : url,
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
data : JSON.stringify(dataObj)
})
.success(function()
{
console.error("Done");
})
.error(function(dataObj)
{
console.error("Error");
});
<强> Symfony的 强>
包括:
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
功能:
/**
* POST Route annotation.
* @Post("/api/login")
*/
public function apiLoginAction(Request $request)
{
$parameters = $request->request->all();