【发布时间】:2018-07-28 15:51:54
【问题描述】:
我发现很难将 http post 请求发送到 laravel api。 GET 方法工作正常,但在 google chrome 控制台中出现以下错误
无法加载http://localhost:8000/api/user/auth:响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:8100” 访问。
我已经 2 天了,因为我正在寻找解决方案。 我在 ionic 官方博客上找到了一个帖子 https://blog.ionicframework.com/handling-cors-issues-in-ionic/ 但它似乎没有帮助。
谢谢
这是我的代码
authUser(email, pass) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
let data = JSON.stringify(
{
"email": email,
"password": pass
}
);
this.http.post(this.API_URL+'user/auth', data, httpOptions).subscribe(data => {
if(data['success']) {
this.user = data['response'];
} else {
this.hasError = data['message'];
console.log(this.hasError);
}
});
}
【问题讨论】:
-
在您的 API 服务器上启用 CORS github.com/barryvdh/laravel-cors
-
感谢@TiepPhan,但我已经在 laravel 中设置了 cors 中间件,并且我的 GET 请求工作正常
-
根据您上面的错误消息,您的服务器没有正确设置为接受 Origin localhost:8100。请验证您的服务器以接受上述来源。
-
感谢@TiepPhan 的帮助,我也会尝试您的解决方案。
标签: angular laravel http ionic2 cors