I have a web application on asp.net that uses Jquery-ajax call to GET some data from a restful service. This WCF service working proper that I can call it in my browser with it's 'UriTemplate' , next my data will be shown.
But, When I using ajax call to get the content, Access-Control-Allow-Origin Error will be logged. Consider that I've been set the Access-Control-Allow-Origin and Header parameters in my web.config file, but didn't get the result.
Error Content:
XMLHttpRequest cannot load http://localhost:8000/MonitorDataProviderService/MonitorData. Origin http://localhost:2265 is not allowed by Access-Control-Allow-Origin.
My ajax call is in a ascx script block:
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
}
Config file tags:
<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>
Also, Adding it on Global.asax wasn't helpful:
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();
}
}
Browsers Used: - Chrome 28 - Firefox 22.0
Any Helpful idea will be appreciated
Best Regards