【发布时间】:2014-10-25 10:21:47
【问题描述】:
我有已实施 CORS 的 WCF 服务。我通过 AJAX POST 从 WCF 中的 localhost:59258 方法调用。如果我在其他端口上通过 localhost 调用此方法 - 它正在工作。如果我将相同的方法称为相同的 WCF 但另一个 URL 我会得到 400 Bad Reqest,请参见下文。我试图通过 Fiddler 调用方法并且它正在工作。哪里有问题?
第一个示例 - 在 AJAX 和 Fiddler 中工作
带有 CORS 的 WCF 在 http://localhost:8733/json/GetId 上侦听
我从http://localhost:59258方法GetId调用
第二个例子 - 不工作在 AJAX 和 工作 在 Fiddler
带有 CORS 的 WCF 在 http://some.domain:8733/json/GetId 上侦听
我从 http://localhost:59258 方法 GetId 调用
这是WCF接口的一部分:
[OperationContract]
[WebInvoke(Method = "*",
UriTemplate = "GetId",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string GetId(string a);
这里是 AJAX 的一部分:
var request = $.ajax({
type: "POST",
url: "http://some.domain:15010/json/GetId",
data: JSON.stringify( { a: "123" }),
error: function (e) {
alert(e.responseText);
},
datatype: "json",
contentType: " text/json; charset=utf-8"
});
提琴手日志 - 无效示例:
【问题讨论】: