2016-05-05 53 views
1

我刚刚创建了我的第一个WCF服务,我想与其他客户端共享。我在IIS,Web.config和Global.asax中添加了Access-Control-Allow-Origin标头,但远程客户端仍不允许访问该服务。我已在Chrome和IE(我们的组织支持的浏览器)中进行了测试。我的错误是“对预检请求的响应未通过访问控制检查:否请求的资源上存在'Access-Control-Allow-Origin'标头。”访问控制允许来源是在IIS中,但不工作

在web.config中:

<httpProtocol> 
    <customHeaders> 
    <add name="Access-Control-Allow-Origin" value="*" /> 
    <add name="Access-Control-Allow-Headers" value="Content-Type"/> 
    <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS"/> 
    </customHeaders> 
</httpProtocol> 

在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(); 
    } 
} 

在这里,他们是在IIS:

Screen shot of IIS

这里的JavaScript的尝试访问服务。它在本地工作,但不是远程:

$.ajax({ 
    type: "POST", 
    url: "http://localhost/wsSamwise/Service1.svc/GetTime", 
    data: '{"UserName": "' + userName + '"}', 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(data) { 
     $("#results").html(data.GetTimeResult); 
     console.log(data); 
    } 
}); 

所有这些想法来自各种Stackoverflow线程,但他们都没有为我工作。我错过了什么?

编辑:我刚刚在本地主机与远程浏览器签出了响应头。我在本地主机的响应中看到Access-Control-Allow-Origin标题,但在远程浏览器的响应中看不到该标头。这就像它没有被发送。

从我的本地主机的响应报头:

Access-Control-Allow-Headers:Content-Type 
Access-Control-Allow-Methods:POST,GET,OPTIONS 
Access-Control-Allow-Origin:* 
Access-Control-Allow-Origin:* 
Cache-Control:private 
Content-Length:82 
Content-Type:application/json; charset=utf-8 
Date:Thu, 05 May 2016 16:01:28 GMT 
Server:Microsoft-IIS/7.5 
X-AspNet-Version:4.0.30319 
X-Powered-By:ASP.NET 

从远程浏览器的响应:

Allow:OPTIONS, TRACE, GET, HEAD, POST 
Content-Length:0 
Date:Thu, 05 May 2016 16:00:09 GMT 
Public:OPTIONS, TRACE, GET, HEAD, POST 
Server:Microsoft-IIS/7.5 
X-Powered-By:ASP.NET 

编辑2:伙计们,也许这是一个IIS问题完全。我只是将自定义标头添加到IIS。它显示在本地主机浏览器中,但不在客户端上。远程浏览器中缺少Body-Count标题,但我在本地浏览器上看到它。

custom header

+0

具体来说,它不工作?它是否超时?提供错误讯息?空的回应? – Brian

+0

编辑为包含错误。 – mrcoulson

+0

您是否检查了[此链接](http://www.productiverage.com/wcf-cors-plus-json-rest-complete-example)? –

回答

0

确保Ajax的URL是正确的。

在发布的原始代码中,我的Ajax请求的URL正在查看localhost,而不是托管该服务的计算机的FQDN。这是问题所在。当然,如果服务没有托管在URL上,那么Ajax请求将不起作用。我错过了错误信息,错过了显而易见的解决方案。

1

使用JSONP是最经典和标准从differente酒庄(跨来源请求共享)工作。 然后查看Access-Control-Allow-Origin以添加特定的限制。

使用这样的JSONP: 新的JSONP功能通过WebHttpBinding公开。对于CustomersService的配置将是这样的:

<bindings> 
    <webHttpBinding> 
     <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" /> 
    </webHttpBinding> 
    </bindings> 
    <services> 
    <service name="ServiceSite.CustomersService"> 
     <endpoint address="" binding="webHttpBinding" 
       bindingConfiguration="webHttpBindingWithJsonP" contract="ServiceSite.CustomersService" 
       behaviorConfiguration="webHttpBehavior"/> 
    </service> 
    </services> 

消费JSONP使用jQuery

// Get the JsonP data 
$.getJSON('http://localhost:65025/CustomersService.svc/GetCustomers?callback=?', null, function (customers) { 
     alert('Received ' + customers.length + ' Customers'); 
}); 

来源: How to natively enable JSONP for existing WCF service?

+0

这将取代客户端应用程序中的'.ajax({})'?我认为'$ .ajax({})'是与Web服务交互的首选方式。 – mrcoulson

+0

当我尝试这样做时,根据其数据类型'endpointBehaviorConfugurationType',我得到'webHttpBehavior'无效的错误。当我运行它时,我得到一个JS错误“不允许的方法”。 – mrcoulson

+0

请尝试以下文档:http://www.bendewey.com/index.php/186/using-jsonp-with-wcf-and-jquery – ArDumez

相关问题