【问题标题】:requesting cloudinary resource gives CORS errors请求 cloudinary 资源会导致 CORS 错误
【发布时间】:2018-10-24 04:31:11
【问题描述】:

我有一个 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.

【问题讨论】:

    标签: php laravel cors cloudinary


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      这是我的 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'));
      

      【讨论】:

        猜你喜欢
        • 2019-11-10
        • 2019-08-05
        • 2015-11-28
        • 2020-03-04
        • 1970-01-01
        • 2013-08-27
        • 1970-01-01
        • 1970-01-01
        • 2020-05-21
        相关资源
        最近更新 更多