[英]CORS on WCF - Why specify web server when request is from client machine?
為了允許對WCF服務的HTTP GET請求,我必須通過在WCF方法中執行以下操作來允許Web服務器訪問:
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", webServer);
為什么行得通? 該請求來自客戶端瀏覽器,而不是Web服務器。
載入頁面
客戶端-HTTP-> WebServer
提出WCF請求
客戶端-HTTP-> WCF服務
我使用Wireshark檢查WCF服務器上的流量。 它確實來自用戶打開了網頁的客戶端計算機。 它不是來自Web服務器。 那么,為什么Access-Control-Allow-Origin
量不是來自Web服務器時,為什么在Access-Control-Allow-Origin
中指定Web服務器卻起作用?
如果有幫助,這是JavaScript中的調用:
var config = {
url: 'http://WcfServerName:8000/MmaWebApiService/CreateCassette',
method: 'POST',
contentType: 'application/json; charset=utf-8',
data: { 'moduleSerialNumbers': [$scope.state.subId1, $scope.state.subId2, $scope.state.subId3] }
};
$http(config)
.then(function (response) {
addCassetteToStack(response.data.d);
$rootScope.setUserMessage('Cassette ' + response.data.d.SerialNumber + ' created.');
}, function (response) {
console.log('err: ', response);
});
我不確定所提供的代碼中webServer的類型是什么,或者屬性是什么,所以我做一些假設。 通常,WCF服務器是受保護的資源,並且應該在該級別上允許或拒絕來自其他源域的訪問,例如Web服務器或javascript代碼。 即使您的JavaScript調用是在客戶端進行的,請求標頭也應該在其中具有用於訪問控制的源域。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.