仍然获得Access-Control-Allow-Origin错误

时间:2013-08-07 05:27:05

标签: c# asp.net jquery cross-domain

我在asp.net上有一个Web应用程序,它使用Jquery-ajax调用 GET 来自restful服务的一些数据。这个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

任何有用的想法将不胜感激

最好的问候

0 个答案:

没有答案