好比 http://www.test.com 和 http://m.test.comhtml
<system.web> <!--其餘配置 省略……--> <httpCookies domain="test.com" /><!--同一頂級域名--> </system.web> <handlers> <!--其餘配置 省略……--> <!--<remove name="OPTIONSVerbHandler" />--><!--這裏必定得要註釋掉OPTIONSVerbHandler。意思容許支持 OPTIONS --> </handlers> <httpProtocol> <!--其餘配置 省略……--> <customHeaders> <add name="Access-Control-Allow-Origin" value="http://www.test.com" /><!-- 容許指定的地址--> <add name="Access-Control-Allow-Credentials" value="true" /><!--容許攜帶Cookie--> <add name="Access-Control-Allow-Methods" value="GET, HEAD, OPTIONS, POST, PUT" /> <add name="Access-Control-Allow-Headers" value="cache-control,content-type,if-modified-since,origin,x-requested-with,content-language" /><!--header支持的都填入,不夠的繼續添加--> </customHeaders> </httpProtocol>
<system.web> <!--其餘配置 省略……--> <httpCookies domain="test.com" /><!--同一頂級域名--> </system.web>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>web
Global.asaxajax
protected void Application_BeginRequest(object sender, EventArgs e) { var app = ((System.Web.HttpApplication)sender); if (!string.IsNullOrWhiteSpace(app.Request.Headers["Origin"])) { app.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); app.Response.AppendHeader("Access-Control-Allow-Origin", app.Request.Headers["Origin"]);//替換自定義便可,如:「http://www.test.com」 if (!string.IsNullOrWhiteSpace(app.Request.Headers["Access-Control-Request-Method"])) { app.Response.AppendHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT"); app.Response.AppendHeader("Access-Control-Allow-Headers", "cache-control,content-type,if-modified-since,origin,x-requested-with,content-language"); app.Response.End(); } } }
//原生請求方式: var xhr = new XMLHttpRequest(); xhr.withCredentials = true; //JQuery 請求方式 $.ajaxSetup({crossDomain: true, xhrFields: {withCredentials: true}});
博客地址: | https://www.cnblogs.com/smartstar/ |
博客版權: | 本文以學習、研究和分享爲主,歡迎轉載,但必須在文章頁面明顯位置給出原文鏈接。若是文中有不妥或者錯誤的地方還望高手的你指出,以避免誤人子弟。若是以爲本文對你有所幫助不如【推薦】一下!若是你有更好的建議,不如留言一塊兒討論,共同進步!再次感謝您耐心的讀完本篇文章。 |