私は、Jquery-ajax 呼び出しを使用して安らかなサービスからデータを取得する asp.net の Web アプリケーションを持っています。この WCF サービスは正常に動作しているため、ブラウザーで ' UriTemplate ' を使用して呼び出すことができます。次にデータが表示されます。
しかし、コンテンツを取得するために ajax 呼び出しを使用すると、Access-Control-Allow-Originエラーがログに記録されます。web.config ファイルで Access-Control-Allow-Origin および Header パラメーターを設定したが、結果が得られなかったとします。
エラー内容:
XMLHttpRequest cannot load http://localhost:8000/MonitorDataProviderService/MonitorData. Origin http://localhost:2265 is not allowed by Access-Control-Allow-Origin.
私のajax 呼び出しは ascx スクリプト ブロックにあります。
function FillDataA()
{
console.log("Address is : " + address);
$.ajax({
type: "GET",
url: address,//Address is valid
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert("WIN");
},
error: function (msg) { console.log("ERROR"); }
});
setTimeout(function () { FillDataA(); }, 2000);//The method is calling continuously
}
構成ファイルのタグ:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:2265" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>
</system.webServer>
また、 Global.asaxに追加しても役に立ちませんでした。
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin",
"*");
// I've Tested fixed url in place of '*' too
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
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();
}
}
使用ブラウザ: - Chrome 28 - Firefox 22.0
役立つアイデアをいただければ幸いです
よろしくお願いします