最近我参与了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();
}
}
但如何实现相同,让拦截器能够正确修复跨域问题?