[英]Access Control Allow Origin issue with WCF service
Error 1:- No 'Access-Control-Allow-Origin' header is present on the requested resource. 错误1:-所请求的资源上没有'Access-Control-Allow-Origin'标头。 Origin ' http://localhost:2021 ' is therefore not allowed access.
因此,不允许访问源' http:// localhost:2021 '。
Error 2:- Response for preflight has invalid HTTP status code 405. 错误2:-飞行前响应具有无效的HTTP状态代码405。
I have done google and know that this is Cross Domain issue by Chrome. 我已经完成了google的工作,并且知道这是Chrome的跨域问题。 So I added the following in my App.Config.
因此,我在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>
and changed the method headers with folowing code. 并使用以下代码更改了方法标题。
[OperationContract]
[WebInvoke(Method = "OPTION", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "AddAddressDirectory")]
Task<string> AddAddressDirectory(string aDVM);
Further I am using the SOAP service 此外,我正在使用SOAP服务
public string AddAddressDirectory(string model)
{
var aDVMObj = JsonConvert.DeserializeObject<AddressDirectoryViewModel>(model);
var result = _directoryServiceClient.AddAddressDirectory(aDVMObj);
var json = JsonConvert.SerializeObject(result);
return json;
}
And I am calling the method from angularJs. 我正在从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) { });
};
Try add in header - methods: 尝试添加标题-方法:
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, OPTIONS" />
or instal this plugin 或安装此插件
add this to your web config file: 将此添加到您的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>
also you can define your method like this: 你也可以这样定义你的方法:
[WebInvoke(Method = "*"
this way you will cover all kinds of request. 这样,您将满足各种要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.