关于webApi 跨域请求

先说一下我的项目,后台单纯的webApi,前端采用Vue+axios,

 

说一下我遇见的问题,axios请求webAPI但是浏览器会报错,大致上就是不允许跨域请求。

于是查找解决办法发现以下代码(web.config)(直接粘贴了)

 <!--以下配置为了让IIS7+支持Put/Delete方法-->
15         <httpProtocol>
16             <customHeaders>
17                 <add name="Access-Control-Allow-Origin" value="*" />
18                 <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
19                 <add name="Access-Control-Allow-Headers" value="Content-Type" />
20             </customHeaders>
21         </httpProtocol>
22         <!--IIS7/7.5上必须加这个配置,否则访问报错--> 
23         <modules>
24             <remove name="WebDAVModule" />
25         </modules>
26         <validation validateIntegratedModeConfiguration="false" />
27         <handlers>
28             <remove name="WebDAV" />
29             <remove name="WebAPI_64bit" />
30             <remove name="WebAPI_32bit" />
31             <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
32             <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
33             <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
34             <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" resourceType="Unspecified" requireAccess="Script" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
35             <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" resourceType="Unspecified" requireAccess="Script" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
36             <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" resourceType="Unspecified" requireAccess="Script" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
37             <add name="WebAPI_64bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv4.0,bitness64" />
38             <add name="WebAPI_32bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv4.0,bitness32" />
39         </handlers>



毫无疑问,网上的大致上都是这写法,然而这种写法对于直接新建webapi项目不适用,尤其是 <handlers></handlers>里面的写法,完全不管用,至少对于我不管用。前端根本访问不了,直接报405错误,
于是正确的修改如下:
 
15         <httpProtocol>
16             <customHeaders>
17                 <add name="Access-Control-Allow-Origin" value="*" />
18                 <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
19                 <add name="Access-Control-Allow-Headers" value="Content-Type" />
20             </customHeaders>
21         </httpProtocol>
22          
23         <modules>
24             <remove name="WebDAVModule" />
25         </modules>
26         <validation validateIntegratedModeConfiguration="false" />
27         <handlers>
28             <remove name="WebDAV" />
29           
39         </handlers>


这样修改是可以跨域请求了 。


留下笔迹,防止忘记







posted on 2017-10-29 17:33  快乐的小帅哥  阅读(722)  评论(0编辑  收藏  举报

导航