1. NuGet下載
microsoft.aspnet.webapi.cors
2 . Web API 路由中
config.EnableCors(new EnableCorsAttribute());
A.web.config (AB任選其一)
一般瀏覽器的跨域(寫在web.config 的 <system.webServer>下)
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
-----------------------------------------------------------
B . Api控制器中(AB任選其一)
谷歌瀏覽器跨域,(寫在api控制器中)
public string Options()
{
return null;
}
-----------------------------------------------------------
using System.Web.Http.Cors;
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class ValuesController : ApiController
{
}
3.api控制器中(2,3任選其一)
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));