【发布时间】:2017-09-20 06:41:50
【问题描述】:
我正在尝试以角度调用 Laravel API:
search(data){
console.log(data);
this.http.get('http://localhost:8000/flight',JSON.stringify(data))
.subscribe(res=>{
console.log(res);
});
}
以上是使用 GET 请求。
search(data){
console.log(data);
this.http.post('http://localhost:8000/flight',JSON.stringify(data))
.subscribe(res=>{
console.log(res);
});
}
但不适用于 POST 请求
这是我得到的错误:
请求中没有“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:4200” 访问。
即使我的 cors middelware 编写正确
return $next($request)->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Credentials', true )
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers',' Origin, Content-Type, X-Auth-Token');
【问题讨论】:
-
这是服务器上的 CORS 问题
-
你能解释一下我是 laravel 和 angular 的新手吗
-
不,先生,我不知道您需要在 lavel api github.com/barryvdh/laravel-cors987654322@ 中启用 CORS
-
但我已经通过编写 cors 中间件启用了它,并且我还在我的 RouteServiceProvider 中添加了它
-
了解您是否真的编写正确的唯一方法是查看开发人员工具中的网络选项卡,看看您的本地服务器是否确实返回了 CORS 标头。