最近、私は 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();
}
}
しかし、同じことをどのように達成するか、インターセプターがクロスドメインの問題を正しく修正できるとしましょう?