最近我参与了 WCF 的创建,当我尝试使用它时遇到了跨域问题,方法是从 $.ajax 函数发送数据。
var dto = { };
dto.Id = 1;
dto.Name = "Test";
var DTO = { 'jsonMesssage': dto };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost/JsonService/ProcessMessage",
data: JSON.stringify(DTO),
dataType: "json",
crossDomain:true,
success: function(result) {
console.log(result.d);
},
error: function(a, b, c) {
console.log(a, b, c);
}
我找到了人们如何为托管在 IIS 上的 WCF 服务解决它的方式,但我还有其他情况,因为 WCF 托管在 .exe 进程中
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
但是如何做到这一点,假设拦截器能够正确修复跨域问题?