[英]How to add CORS headers for OPTIONS preflight request in Laravel Lumen 5.5.2 with angular4 as front end
[英]CORS - Lumen and Angular request
我有一個在http://localhost:4200 上運行的 Angular 應用程序和一個在http://localhost:8000 上運行的 Lumen API。
我正在使用 barryvdh/laravel-cors,並且我所有的 POSTS 請求都返回“請求的資源上不存在‘Access-Control-Allow-Origin’標頭。”
有什么線索嗎? 我的 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());
}
}
所有 GET 請求都正常工作。
啟用 apache mod_headers 將解決這個問題。 運行以下命令
這取決於您傳遞的數據以及您在后端的處理方式。 你有沒有嘗試過對數據進行字符串化
JSON.stringify(data)
post(url: string, data) {
return this.http.post("http://localhost:8000/"+ url, JSON.stringify(data))
.map(response => response.json());
}
您需要在 Lumen 上授予對 Angular 服務器的訪問權限
如果您不使用 CORS 只需在 /bootstrap/app.php 上添加這一行
header('Access-Control-Allow-Origin: http://localhost:4200/');
或為每個人:
header('Access-Control-Allow-Origin: *');
如果您使用 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.