我在使用Laravel 5时遇到了麻烦。我获得了csrf令牌,但我无法发帖。为了注册目的,我试着发帖。但它显示出令牌不匹配。
发布方法
var registerData = {
'name':customer.name,
'email':customer.email,
'password' :customer.password,
'_token' : customer._token
};
alert(registerData._token);
$http({
method : 'POST',
url : 'http://192.168.0.112/grubsouk/grub/customer',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data : registerData
})
.success(function(data){
console.log(data);
alert("success");
$location.url("/sign");
})
.error(function(data){
alert("error");
})
获取方法
$http.get('http://192.168.0.112/grubsouk/admin/getcsrf')
.success(function(data, status, headers, config){
alert("**** SUCCESS ****");
alert(data.token);
$rootScope.token = data.token;
$rootScope.customer = {
_token : $rootScope.token
}
alert(data.token);
})
.error(function(data, status, headers, config){
alert("**** ERROR ****");
alert(status);
})
$state.go('registration');
答案 0 :(得分:0)
在控制器中添加以下内容
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");