запрашивающий облачный ресурс дает ошибки CORS

У меня есть сервер laravel / lumen, управляющий загрузкой моих облачных ресурсов. Я также использую сервер в качестве конечной точки API для моего приложения переднего плана. Одна из конечных точек возвращает файл из Cloudinary. Я делаю это, перенаправляя запрос на Cloudinary ресурс. Однако мое приложение не работает, потому что в перенаправленном ресурсе нет заголовков CORS.

return redirect()->to("https://res.cloudinary.com/gates/raw/upload/" . $upload->id);

ошибка, которую я получаю:

Redirect from 'https://{my-server.com}/api/v1/export' to
'https://res.cloudinary.com/gates/raw/upload/{upload-id}' has been
blocked by CORS policy: No 'Access-Control-Allow-Origin' header is
present on the requested resource. Origin 'https://{my-frontend.com}'
is therefore not allowed access.

-1

Решение

Вы можете использовать эту библиотеку на вашем сервере: https://github.com/barryvdh/laravel-cors

return [
/*
|--------------------------------------------------------------------------
| Laravel CORS
|--------------------------------------------------------------------------
|
| allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
| to accept any value.
|
*/
'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedHeaders' => ['Content-Type', 'X-Requested-With'],
'allowedMethods' => ['*'], // ex: ['GET', 'POST', 'PUT',  'DELETE']
'exposedHeaders' => [],
'maxAge' => 0,
]


allowedOrigins => [*] mean that you give access to your server, you can add ip's or dns for restrict access to your server
0

Другие решения

Вот мое промежуточное ПО CORS:

use Closure;

class CORS {

/**
* Handle an incoming request.
*
* @param  \Illuminate\Http\Request  $request
* @param  \Closure  $next
* @return mixed
*/
public function handle($request, Closure $next)
{

header("Access-Control-Allow-Origin: *");

// ALLOW OPTIONS METHOD
$headers = [
'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Headers'=> 'Content-Type, X-Auth-Token, Origin'
];
if($request->getMethod() == "OPTIONS") {
// The client-side application can set only headers allowed in Access-Control-Allow-Headers
return Response::make('OK', 200, $headers);
}

$response = $next($request);
foreach($headers as $key => $value)
$response->header($key, $value);
return $response;
}

}

Чтобы использовать промежуточное программное обеспечение CORS, вы должны сначала зарегистрировать его в своем
Файл app \ Http \ Kernel.php выглядит так:

protected $routeMiddleware = [
//other middlewares
'cors' => 'App\Http\Middleware\CORS',
];

Тогда вы можете использовать его в своих маршрутах

Route::get('example', array('middleware' => 'cors', 'uses' => 'ExampleController@dummy'));
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector