关于跨域处理

在全局事件添加以下代码 拦截所有请求添加跨域头

  1. public static function onRequest(Request $request, Response $response): bool
  2. {
  3. // TODO: Implement onRequest() method.
  4. $response->withHeader('Access-Control-Allow-Origin', '*');
  5. $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
  6. $response->withHeader('Access-Control-Allow-Credentials', 'true');
  7. $response->withHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
  8. if ($request->getMethod() === 'OPTIONS') {
  9. $response->withStatus(Status::CODE_OK);
  10. $response->end();
  11. }
  12. return true;
  13. }