1:首先在webconfig 中添加
<system
.webServer
>
<httpProtocol
>
<customHeaders
>
<!--响应类型
(值为逗号分隔的一个字符串,表明服务器支持的所有跨域请求方法
)-->
<add name
="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS"/>
<!--响应头设置
(Content
-Type
:只限于三个值application
/x
-www
-form
-urlencoded
,multipart
/form
-data
,text
/plain
)-->
<add name
="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>
<!--如果设置Access
-Control
-Allow
-Origin
:*,则允许所有的域名脚本访问该资源
-->
<add name
="Access-Control-Allow-Origin" value="*"/>
<!--<add name
="Access-Control-Allow-Origin" value="http://deo.com,http://mod.com"/> 设置允许跨域访问的网址
-->
</customHeaders
>
</httpProtocol
>
</system
.webServer
>
2: 在Global 中添加一个方法
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration
.RegisterAllAreas();
WebApiConfig
.Register(GlobalConfiguration
.Configuration
);
FilterConfig
.RegisterGlobalFilters(GlobalFilters
.Filters
);
RouteConfig
.RegisterRoutes(RouteTable
.Routes
);
BundleConfig
.RegisterBundles(BundleTable
.Bundles
);
}
protected void Application_BeginRequest()
{
if(Request
.Headers
.AllKeys
.Contains("Origin")&&Request
.HttpMethod
=="OPTIONS")
{
Response
.Flush();
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-485366.html