【发布时间】:2018-08-03 08:01:31
【问题描述】:
我正在制作一个 API,目前登录 POST 请求运行良好
$http({
url:'http://myurl/public/login',
method:"POST",
data:{usuario:$scope.usuario, password:$scope.password}
})
.then(function(response){
//exito.
if (response.data.status=="ok"||response.data.status==ok||response.data.status=='ok'){
$cookies.put('jwtToken', response.data.token);
$cookies.put('cliente_id', response.data.cliente_id);
myNavigator.pushPage('components/inicio/inicio.html');
}
})
.then(function(response){
//fallo
if (response.data.status=="error"||response.data.status==error||response.data.status=='error'){
ons.notification.alert("Usuario y/o contraseña incorrecta");
}
我的登录没有问题,但是当我尝试执行 GET 请求时
var cliente_id = $cookies.get('cliente_id');
$http({
method:"GET",
url:"http://myurl/public/informacion/"+cliente_id,
headers:{'Authorization':'Bearer'+' '+ $cookies.get('jwtToken')}
})
.then(function(response){
console.log(response.data);
})
.then(function(response){
console.log("fallo");
})
我收到这个错误
Failed to load http://myurl/public/informacion/1376: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 500.
我的服务器上有这个
$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization, token')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
});
我在某处读到 Authorization 的标题我需要 OPTIONS 并且我确实拥有它,所以如果有人知道为什么会发生这种情况,请帮助。
【问题讨论】:
-
"响应的 HTTP 状态代码为 500。" — 你需要调试你的 500 错误。
-
“我在某处读到,对于带有授权的标头,我需要 OPTIONS,我确实拥有它”——错误消息没有提到预检,因此选项似乎不是问题请求。
-
@jaime 你试过添加 header('Access-Control-Allow-Credentials: true');
-
@BinitGhetiya 等一下我正在尝试,因为我添加了一些代码,现在登录也不起作用哈哈哈
-
你在使用 chrome 吗?然后尝试安装chrome.google.com/webstore/detail/cors-toggle/…