IIS CORS,基于JavaScript获取请求
我在使用不同配置的IIS CORS,基于JavaScript获取请求,iis,axios,cors,Iis,Axios,Cors,我在使用不同配置的axios或fetch时遇到CORS问题,以下是我的代码: 对于fetch: fetch(url, { method: 'GET', mode: 'cors', headers: { Authorization: token, 'Access-Control-Allow-Origin': '*'} }) .then(response => response.ok ? response : Promise.
axios
或fetch
时遇到CORS问题,以下是我的代码:
对于fetch
:
fetch(url, {
method: 'GET',
mode: 'cors',
headers: {
Authorization: token,
'Access-Control-Allow-Origin': '*'}
})
.then(response =>
response.ok ? response : Promise.reject<Response>(response)
)
.then(response => response.json())
.then(data => data.Token)
在我的web.config中,我有:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
我还应该尝试什么?正如作者所发表的评论,IIS CORS是处理CORS问题的一个好选择。某些预飞行请求无法正确处理。在Asp.net web应用程序中,我们通常添加一个global.asax
文件,以便在服务器端对其进行处理
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
Response.End();
}
}
但是,IIS核心模块是更好的解决方案。
1.在服务器端安装IIS CORS模块。
2.这是一个通用配置,允许来自所有来源的所有请求
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
<cors enabled="true" failUnlistedOrigins="true">
<add origin="*" allowed="true">
<allowHeaders allowAllRequestedHeaders="true"/>
</add>
</cors>
</system.webServer>
</configuration>
如果有什么我可以帮忙的,请随时告诉我。您的方法是错误的,因为它无法正确处理匿名飞行前请求。请使用IIS CORS模块,
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
<cors enabled="true" failUnlistedOrigins="true">
<add origin="*" allowed="true">
<allowHeaders allowAllRequestedHeaders="true"/>
</add>
</cors>
</system.webServer>
</configuration>