From fd4d485bdb91f3da3beac2797cee15b7b63b398d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=97=E7=A5=9E?= Date: Mon, 22 Nov 2021 10:20:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0CORS=20https://stackoverflow.?= =?UTF-8?q?com/questions/25437405/cors-access-for-httplistener?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JT808.Gateway/JT808HttpServer.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/JT808.Gateway/JT808HttpServer.cs b/src/JT808.Gateway/JT808HttpServer.cs index 12cd6a7..b85cb52 100644 --- a/src/JT808.Gateway/JT808HttpServer.cs +++ b/src/JT808.Gateway/JT808HttpServer.cs @@ -76,6 +76,15 @@ namespace JT808.Gateway context.Http404(); continue; } + // 增加CORS + // https://stackoverflow.com/questions/25437405/cors-access-for-httplistener + if (context.Request.HttpMethod == HttpMethod.Options.Method) + { + context.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With"); + context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); + context.Response.AddHeader("Access-Control-Max-Age", "1728000"); + } + context.Response.AppendHeader("Access-Control-Allow-Origin", "*"); if (authorization.Authorization(context, out var principal)) { await ProcessRequestAsync(context, principal);