[英]CORS - Lumen and Angular request
I have an Angular app running at http://localhost:4200 and a Lumen API running at http://localhost:8000 .我有一个在http://localhost:4200 上运行的 Angular 应用程序和一个在http://localhost:8000 上运行的 Lumen API。
I'm using barryvdh/laravel-cors and all my POSTS requests returns "No 'Access-Control-Allow-Origin' header is present on the requested resource."我正在使用 barryvdh/laravel-cors,并且我所有的 POSTS 请求都返回“请求的资源上不存在‘Access-Control-Allow-Origin’标头。”
Any clues about that?有什么线索吗? My Angular Database Service:我的 Angular 数据库服务:
@Injectable()
export class DatabaseService {
constructor(private http: Http) { }
get(url: string) {
return this.http.get("http://localhost:8000/" + url)
.map(response => response.json());
}
post(url: string, data) {
return this.http.post("http://localhost:8000/"+ url, data)
.map(response => response.json());
}
}
All GET requests work properly.所有 GET 请求都正常工作。
Enabling apache mod_headers will fix this issue.启用 apache mod_headers 将解决这个问题。 Run below commands运行以下命令
It depends on the data you pass and how you handle in the back-end.这取决于您传递的数据以及您在后端的处理方式。 did you try stringifying data你有没有尝试过对数据进行字符串化
JSON.stringify(data)
post(url: string, data) {
return this.http.post("http://localhost:8000/"+ url, JSON.stringify(data))
.map(response => response.json());
}
You need to grant access to your Angular server on Lumen您需要在 Lumen 上授予对 Angular 服务器的访问权限
If you are not using CORS Just add this line on /bootstrap/app.php如果您不使用 CORS 只需在 /bootstrap/app.php 上添加这一行
header('Access-Control-Allow-Origin: http://localhost:4200/');
or for everyone:或为每个人:
header('Access-Control-Allow-Origin: *');
If you are using CORS you must set the same thing on the headers on App\\Http\\Middleware\\CorsMiddleware (or whatever is called your CORS file)如果您使用 CORS,您必须在 App\\Http\\Middleware\\CorsMiddleware(或任何称为您的 CORS 文件)的标头上设置相同的内容
$headers = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => '86400',
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With'
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.