请求的资源上不存在“Access-Control-Allow-Origin”标头

时间:2014-04-11 10:30:41

标签: jquery asp.net wcf google-chrome firefox

我的ASP.NET网页中的JavaScript试图访问WCF服务。在IE中工作正常,但我在chrome和firefox浏览器中收到错误。下面是我的jquery脚本。

    $.getJSON(url, function (result) {
    if (result.d.length > 0) {
        $.each(result.d, function (i, item) {
            $("[id$='dateStamp']").text("Last updated On : " + this.LoadDateTime.DateWCF('dd-MMM-yyyy'));
        });
    }
});

我甚至尝试使用$.ajax但没有用。我尝试了以下选项,但没有任何效果

<system.webServer>
<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, 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", "*");

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            //These headers are handling the "pre-flight" OPTIONS call sent by the browser
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }

    }

0 个答案:

没有答案
相关问题