IIS ajax CORS 訪問發送自訂Header時分析

來源:互聯網
上載者:User

標籤:dex   headers   type   pos   als   tran   and   ica   method   

情境:

a站:http://127.0.0.1/a 

b站:http://localhost/b (IIS7.5)

a站用ajax訪問b的資源,代碼如下:

$.ajaxSetup({ xhrFields: { withCredentials: true } });//帶cookie,不是重點

$.ajax({

  url:"http://localhost/b/home/index",

  header:{myheader:"hello"},//自訂頭將讓瀏覽器把此次訪問當做複雜請求,會先用OPTIONS做 預請求(preflight request)

     success:function(data){

    alert(data);

}

  

});

b站後台:

(1)必須要設定的回應標頭:

protected void Application_BeginRequest(object sender, EventArgs e)
{
var response = this.Context.Response;

response.Headers["Access-Control-Allow-Origin"] = "http://127.0.0.1";

response.Headers["Access-Control-Allow-Credentials"] = "true";
response.Headers["Access-Control-Allow-Methods"] = "POST,GET,PUT,OPTIONS";
response.Headers["Access-Control-Allow-Headers"] = "myheader";

}

如果一切正常,應該正常調用 alert(data);

但,對,確實沒有調用,回應標頭 Access-Control-Allow-Origin 沒有返回給瀏覽器,預請求沒有通過。

(2)webconfig中:

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>

<remove name="OPTIONSVerbHandler" />

<!--<remove name="OPTIONS" /> iis 8 -->

 

<add name="OPTIONSVerbHandler" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

把(2)加上,一切正常了,alert(data)被調用。

於是,懷疑OPTIONSVerbHandler這個處理器有bug。

但始終沒找到代碼。

 

IIS ajax CORS 訪問發送自訂Header時分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.