1

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

A.Mokhtari
  • 451
  • 5
  • 16
  • did u read about JSONP.http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about.it may solve your problem – Prabhu Murthy Aug 07 '13 at 05:29
  • Just right now, I read the article... What does make sense? Where should I change my code to get JSONP? Can you prepare some code, please? – A.Mokhtari Aug 07 '13 at 05:54
  • What is the language used to write the AJAX response? The header response changes need to go in that server part and not in the ASP website. (assuming you aren't using ASP to serve the AJAX response) – dab Aug 07 '13 at 06:35
  • can you remove the custom header code from web config. it may works – Innovative Thinker Aug 09 '16 at 14:50

0 Answers0