提问人:SpencerAl 提问时间:11/7/2023 最后编辑:user692942SpencerAl 更新时间:11/9/2023 访问量:92
ASP Classic/CORS - 设置 CORS 标头时不报告标头
ASP Classic / CORS - Reporting no header when header for CORS is set
问:
提前道歉,我是 CORS 的新手,所以可能是我错过的东西。
我正在尝试在当前的 localhost 中设置从一个 localhost 端口到另一个 localhost 端口的 CORS。我已经确认使用相同的语言/框架构建的两个应用程序是可能的,但我遇到了两种不同语言/框架的问题。
目前,每个 localhost 端口都是这样构建的——
本地主机 1:React/.Net C#
本地主机 2:React/.Net C#
Localhost3:ASP 经典
我能够在 localhost 1 和 2 之间完成 CORS,但在处理 localhost2 和 localhost 3 之间的 CORS 时,我的标头不断收到与“Access-Control-Allow-Origin”相关的错误。
下面是我当前尝试将数据发布到的 ASP Classic 文件,该文件用于确认正在发送数据 -
<%
Response.AddHeader "Access-Control-Allow-Origin", "*"
Response.AddHeader "Access-Control-Allow-Methods", "POST, OPTIONS"
Response.AddHeader "Access-Control-Allow-Headers", "Content-Type"
Response.AddHeader "Access-Control-Allow-Credentials", "true"
If Request.ServerVariables("REQUEST_METHOD") = "OPTIONS" then
' Handling preflight request
Response.AddHeader "Access-Control-Allow-Origin", "*"
Response.AddHeader "Access-Control-Allow-Methods", "POST, OPTIONS"
Response.AddHeader "Access-Control-Allow-Headers", "Content-Type"
Response.AddHeader "Access-Control-Allow-Credentials", "true"
Response.Status = 200
Response.End
End If
' Check if it's a POST request
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
Dim requestData, id1
On Error Resume Next
Set requestData = JSONParse(Request.Form("data")) ' Assuming "data" is the key where JSON is sent
On Error Goto 0 ' Re-enable default error handling
If Err.Number <> 0 Then
' Handle JSON parsing errors
Response.Status = 400 ' Bad Request
Response.Write "Error while parsing JSON: " & Err.Description
Response.End
End If
id1 = requestData("id1")
If id1 = "" Or Not IsNumeric(id1) Then
' Invalid or missing data in the JSON payload
Response.Status = 400 ' Bad Request
Response.Write "Invalid or missing ID data"
Response.End
Else
' Respond with a confirmation message
Response.AddHeader "Content-Type", "application/json" ' Set appropriate Content-Type
Response.Write "{ ""message"": ""Received the data successfully"" }" ' Respond with JSON confirming data receipt
Response.End
End If
Else
' Handling non-POST requests
Response.Status = 405 ' Method Not Allowed
Response.Write "Only POST requests are allowed"
Response.End
End If
%>
以下是我在 Chrome 中查看 devTools/issues 时收到的消息。
我错过了什么/我该如何解决这个问题?
提前感谢您:)的任何帮助
旁注 - 是的,我知道 * 是一个安全问题,我只是将通配符用于测试目的。
答:
这将是由于 IIS 在请求到达 ASP 页之前使请求短路而发生的。
默认情况下,经典 ASP ISAPI 处理程序将仅接受 ,并且作为有效的 HTTP 谓词并使用其他谓词 (如 将导致 HTTP 状态响应为 )。若要使用,需要在 IIS 中经典 ASP 网站的处理程序映射中编辑 ASPClassic IsapiModule 的 HTTP 处理程序(请参见下图)GET
HEAD
POST
OPTIONS
405 Method Not Allowed
OPTIONS
并添加到以逗号分隔的允许动词列表中。OPTIONS
我发现了我的问题。
我必须在 IIS 的 HTTP 标头部分中设置 CORS 标头,以允许 CORS 发布到我的本地主机,而不是在 ASP 经典代码的单个文件中设置详细信息。
我相信这是由于服务器级别的问题,而不是与代码相关的问题。
评论
OPTIONS
评论
Access to fetch at 'http://localhost/mainsite/arrival.asp' from origin 'https://localhost:44338' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.