markdown 带有CORS和OPTIONS请求的流明

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 带有CORS和OPTIONS请求的流明相关的知识,希望对你有一定的参考价值。

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

/**
 * If the incoming request is an OPTIONS request
 * we will register a handler for the requested route
 */
class CatchAllOptionsRequestsProvider extends ServiceProvider {

  public function register()
  {
    $request = app('request');

    if ($request->isMethod('OPTIONS'))
    {
      app()->options($request->path(), function() { return response('', 200); });
    }
  }

}
<?php namespace App\Http\Middleware;

class CorsMiddleware {

  public function handle($request, \Closure $next)
  {
    $response = $next($request);

    $response->header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, PATCH, DELETE');
    $response->header('Access-Control-Allow-Headers', $request->header('Access-Control-Request-Headers'));
    $response->header('Access-Control-Allow-Origin', '*');

    return $response;
  }

}
Register the `CatchAllOptionsRequestsProvider` service provider in `bootstrap/app.php` which will check the incoming request and response successfully if it is an `OPTIONS` request.

Add the `CorsMiddleware` to the `$app->middleware([` array in `bootstrap/app.php` which will attach the following CORS headers to all responses:

* allow all headers
* allow requests from all origins
* allow all the headers which were provided in the request

以上是关于markdown 带有CORS和OPTIONS请求的流明的主要内容,如果未能解决你的问题,请参考以下文章

带有泽西岛的 OPTIONS 请求上的 CORS 标头

CORS 因请求而失败,带有 OPTIONS(状态为 0 的响应)

ZF2中的CORS POST请求变成OPTIONS请求

CORS,防止带有授权标头的请求预检

Domino、CORS 和 OPTIONS 请求

带有授权标头的 GET 请求之前的 OPTIONS 请求在苗条框架 4 中不起作用