標籤: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時分析