【问题标题】:JayData cannot read WCF data service because of CORS由于 CORS,JayData 无法读取 WCF 数据服务
【发布时间】:2014-05-06 13:06:06
【问题描述】:

我创建了一个 WCF 数据服务并在我的网页中设置了以下内容:

 var mongo = new MyCtx.MyContext({ name: 'oData', oDataServiceHost: 'http://xxxxxxxxx/MongoDataService.svc/', enableJSONP: false });

在我的 WCF 数据服务 web.config 中:

 <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Max-Age" value="3600" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, MaxDataServiceVersion" />
        <add name="Access-Control-Allow-Methods" value="PUT, POST, GET, DELETE, MERGE, OPTIONS" />
      </customHeaders>
    </httpProtocol>

当我尝试打开它时,我收到以下错误:

SEC7118:  http://xxxxxxxxxxxxxx/MongoDataService.svc//DataTable1 " XMLHttpRequest for [URL] required Cross Origin Resource Sharing(CORS). 
SEC7119: "XMLHttpRequest for [URL] required CORS preflight."
SCRIPT7002: XMLHttpRequest: Network problem 0x80070005, Access denied.

我的 IIS 服务器定义了以下标头:

我还应该做什么?

【问题讨论】:

  • 就我而言,我只添加了 并且它可以工作。也许您应该尝试删除其他人?
  • @QianLi 我修改了它,但没有任何反应。它仍然希望启用 CORS。我可以完全访问 IIS 服务器,但无法启用这么小的东西...

标签: jquery cors wcf-data-services jaydata


【解决方案1】:

添加您的 WCF 数据服务文件 Global.asax(如果您还没有)。 然后处理 BeginRequest 事件,并添加代码:

  protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, MERGE" );
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, MaxDataServiceVersion, DataServiceVersion, Authorization");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }

然后它应该工作:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    相关资源
    最近更新 更多