Laravel CORS with Fruitcake
I make react project with laravel Back-end ... I have a CORS problem, I do everything like on link below, with fruitcake.
Laravel 6 CORS policy issue with API but still not working.
cors.php:
'paths' => ['api/*'],
/*
* Matches the request method. `[*]` allows all methods.
*/
'allowed_methods' => ['*'],
/*
* Matches the request origin. `[*]` allows all origins.
*/
'allowed_origins' => ['*'],
/*
* Matches the request origin with, similar to `Request::is()`
*/
'allowed_origins_patterns' => [],
/*
* Sets the Access-Control-Allow-Headers response header. `[*]` allows all headers.
*/
'allowed_headers' => ['*'],
/*
* Sets the Access-Control-Expose-Headers response header.
*/
'exposed_headers' => false,
/*
* Sets the Access-Control-Max-Age response header.
*/
'max_age' => false,
/*
* Sets the Access-Control-Allow-Credentials header.
*/
'supports_credentials' => false,
And, kernel middle-ware is:
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\Fruitcake\Cors\HandleCors::class,
];
what else could be the problem?
여기 사용할 때 사용하는 몇 가지 gotchas가 있다.fruitcake/laravel-cors
:
- 놓다
HandleCors
맨 위에 있는 미들웨어$middleware
에app/Http/Kernel.php
:
protected $middleware = [
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
Putting it at the bottom or somewhere between won't work because requests might be rejected by other middlewares with higher priority.
- Do NOT die or exit in controller.
For example the following won't work:
Route::get('/cors-test', function() {
dd("This won't work");
});
왜냐하면.Fruitcake\Cors\HandleCors::handle
메소드는 요청 처리 후 관련 헤더 추가:
public function handle($request, Closure $next)
{
// --- omitted
// Handle the request
$response = $next($request); // <--- if you die here
if ($request->getMethod() === 'OPTIONS') {
$this->cors->varyHeader($response, 'Access-Control-Request-Method');
}
// you will never reach here
return $this->addHeaders($request, $response);
}
dump
역시 효과가 없다.
- 변경 후 구성 캐시 지우기
app/config/cors.php
:
$ php artisan config:cache
그Fruitcake\Cors\HandleCors::class
귀찮다어디서든 제거하고 이 세 개의 머리글을 에 추가하십시오.api.php
맨 위에 있는 경로 파일
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token, Authorization, Accept,charset,boundary,Content-Length');
header('Access-Control-Allow-Origin: *');
추가하다credentials: 'same-origin'
리액트 앱의 요청 헤더로
Actually ,just remove dd and die command from you code.
php artisan config:clear
php artisan route:clear
php artisan cache:clear
Make sure your permissions are setup correctly (eg. storage is writable)
ReferenceURL : https://stackoverflow.com/questions/60168052/laravel-cors-with-fruitcake
'Programing' 카테고리의 다른 글
팬더에서 특정 열 이름 바꾸기 (0) | 2022.03.15 |
---|---|
Python에서 사전 키를 목록으로 반환하는 방법? (0) | 2022.03.15 |
Android Phone에서 Resact Native 앱을 실행하는 방법 (0) | 2022.03.15 |
사전을 복사하고 사본만 편집하는 방법 (0) | 2022.03.15 |
RxJS 6 페이지가 활성화되지 않은 경우 일시 중지 또는 버퍼 관찰 가능 (0) | 2022.03.14 |