【发布时间】:2020-01-29 17:28:54
【问题描述】:
我正在尝试使用 WCF 启用 CORS。 但是我得到 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource。因此,Origin 'http://localhost' 不允许访问。响应的 HTTP 状态代码为 404。
这是我的 Global.asax
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("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();
}
}
这是我的 web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="TransportSecurity">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="RESTAPI03.testAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="RESTAPI03.testAspNetAjaxBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="RESTAPI03.test">
<endpoint address="" behaviorConfiguration="RESTAPI03.testAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="TransportSecurity" contract="RESTAPI03.Hotels" />
</service>
</services>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint>
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
这是我的应用程序项目中的代码
const httpOptions = {
headers: new HttpHeaders({
'Access-Control-Allow-Origin' : '*'
})
}
return this.httpClient.get('https://www.example/1' httpOptions ).subscribe(data => {
console.log("s");
console.log(JSON.stringify(data));
}, err => {
console.log("err => ");
console.log( JSON.stringify(err));
});
但仍然无法正常工作。我错过了什么?
【问题讨论】:
-
尝试在您的客户端代码中添加 Content-Type 和 Accept 标头。另外,您是否正在发送带有凭据的请求?因为在这种情况下,Access-Control-Allow-Origin 设置为 '*' 会引发错误
-
如何发送带凭证的请求?我把这样的凭证弄错了。 const httpOptions = {headers: new HttpHeaders({ 'Content-Type':'application/json','Accept':'application/json','Access-Control-Allow-Origin' : '*'}), withCredentials:'true'}
-
withCredentials:'true' 表示您正在发送 cookie、身份验证等凭据。在这种情况下,您的服务器不应将 Acces-Control-Allow-Origin 设置为“*”。它应该只允许一个域。稍后我会发布详细的答案和绕过 CORS 限制的方法。
标签: wcf ionic-framework