【发布时间】:2019-10-13 01:02:28
【问题描述】:
我正在尝试使用来自另一个项目的 Http POST 请求调用 WCF 服务方法,它允许 GET 请求,但是当我尝试 POST 请求时,它显示 “不允许方法”。
当我尝试时,它在我的控制台中显示此错误:
从源“http://localhost:6538”访问“http://localhost:7280/api/values/24”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。
网络 API: 方法:
[WebInvoke(UriTemplate = "/values", Method = "POST", ResponseFormat = WebMessageFormat.Json), CorsEnabled]
void AddValue(string value);
方法实现
public void AddValue(string value)
{
string id = nextId.ToString();
nextId++;
allValues.Add(id, value);
string location = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.ToString() + "/" + id;
WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(new Uri(location));
}
客户端应用程序:
$("#post").click(function () {
var data = "\"" + $("#value").val() + "\"";
$.ajax({
url: valuesAddress,
type: "POST",
contentType: "application/json",
data: data,
success: function (result) {
$("#result").text(result);
},
error: function (jqXHR, textStatus, errorThrown) {
$("#result").text(textStatus);
}
});
});
我关注了 https://code.msdn.microsoft.com/Implementing-CORS-support-c1f9cd4b/view/SourceCode#content 我将 Web API 和客户端应用程序创建为不同的项目。 请帮忙解决这个问题。
【问题讨论】:
-
对于 wcf 中的 cors 配置,您可以尝试在 global.asax 的 Application_BeginRequest 中添加 cors 头和允许选项方法,请参考https://www.codeproject.com/Articles/845474/Enabling-CORS-in-WCF
-
我正在尝试使用来自另一个项目的 Http POST 请求调用 WCF 服务方法。我尝试使用 codeproject.com/Articles/845474/Enabling-CORS-in-WCF 中的解决方案,但没有奏效。您能否提出任何其他解决方案。
标签: c# asp.net angularjs wcf .net-framework-version