簡體   English   中英

WCF服務的訪問控制允許起源問題

[英]Access Control Allow Origin issue with WCF service

  • 我是WCF Restful Service的新手,正在UI中使用WCF項目服務並收到以下錯誤*

錯誤1:-所請求的資源上沒有'Access-Control-Allow-Origin'標頭。 因此,不允許訪問源' http:// localhost:2021 '。

錯誤2:-飛行前響應具有無效的HTTP狀態代碼405。

我已經完成了google的工作,並且知道這是Chrome的跨域問題。 因此,我在App.Config中添加了以下內容。

<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>

並使用以下代碼更改了方法標題。

[OperationContract]
[WebInvoke(Method = "OPTION", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "AddAddressDirectory")]
Task<string> AddAddressDirectory(string aDVM);

此外,我正在使用SOAP服務

public string AddAddressDirectory(string model)
{
var aDVMObj = JsonConvert.DeserializeObject<AddressDirectoryViewModel>(model);
var result = _directoryServiceClient.AddAddressDirectory(aDVMObj);
var json = JsonConvert.SerializeObject(result);
return json;
}

我正在從angularJs調用該方法。

var _apiPost = function (url, data, success, failure) {
var req = {
url: serviceUrl + url,
method: 'OPTIONS',
headers: {
'Content-Type': "application/json"
},
dataType: "json",
//crossDomain: true,
//processData: true,
data: JSON.stringify(data)
//data: $httpParamSerializer(JSON.stringify(data))
};
$http(req).then(success, function (response) { });
};

嘗試添加標題-方法:

<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, OPTIONS" />

或安裝此插件

將此添加到您的Web配置文件:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*"/>
    <add name="Access-Control-Allow-Headers" value="X-Requested-With,Content-Type, Accept" />
  </customHeaders>
</httpProtocol>

你也可以這樣定義你的方法:

[WebInvoke(Method = "*"

這樣,您將滿足各種要求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM