DJB*_*DJB 8 c# asp.net asp.net-web-api angularjs
我正在尝试从AngularJS页面连接到ASP.NET Web-API Web服务,我得到以下内容
凭据标志为"true",但"Access-Control-Allow-Credentials"标头为"".允许凭据必须为"true".原产地" 的http://本地主机:221 "因此不允许访问.
var cors = new EnableCorsAttribute("http://localhost:221", "*","GET,PUT,POST,DELETE");
config.EnableCors(cors);
Run Code Online (Sandbox Code Playgroud)
使用此AngularJS
$http({
method: 'GET',
url: 'http://localhost:1980/api/investors/62632',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
withCredentials: true
// withCredentials: true,
}).then(function onUserComplete(response) {
// this callback will be called asynchronously
// when the response is available
}, function onError(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
Run Code Online (Sandbox Code Playgroud)
阅读了很多文章后,我将其添加到web.config中
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:221" />
</customHeaders>
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息
'Access-Control-Allow-Origin'标头包含多个值localhost:221,localhost:221,但只允许一个.原因localhost:221因此不允许访问.
这真的没有任何意义,因为我已经添加了一次,它没有找到它,但我将它添加到web.config并得到一个错误说它被添加了多次.我看了很多文章,似乎找不到合适的答案.我正在使用Google Chrome.非常感谢你的帮助,因为我现在正在拔头发.
Rom*_*n O 25
对谁,谁使用WebApiConfig.cs:
config.EnableCors(new EnableCorsAttribute("*", "*", "*") { SupportsCredentials = true });
Run Code Online (Sandbox Code Playgroud)
ozk*_*ary 10
标题由代码添加两次,另一个由web.config添加.CORS支持用于允许为CORS目的添加标头.配置自定义标头还会为任何请求添加响应标头,因此您可能希望删除配置设置.
var cors = new EnableCorsAttribute..
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:221" />
</customHeaders>
Run Code Online (Sandbox Code Playgroud)
由于这两个区域都添加了两次相同的原点,因此您可以在标题上获得多个值.
使用参数进行AJAX调用时withCredentials: true,响应头应具有Access-Control-Allow-Credentials = true.您需要通过代码使用SupportsCredentials = trueCORS属性添加它.否则,您将收到错误
"Credentials flag is'true',但'Access-Control-Allow-Credentials is''"
有关更多信息,请参阅withCredential参数和响应标头,请参阅本文:
http://www.ozkary.com/2015/12/api-oauth-token-access-control-allow-credentials.html
希望能帮助到你.
对于预检请求,请尝试此处概述的方法:
并使用 Chrome 扩展 Postman 或 Fiddler 来更轻松地调试 CORS。我敢打赌,您将添加标头两次,但如果没有完整的代码,就很难调试。(哎呀,即使使用代码,CORS 也很难调试)。
对我来说,您似乎不应该同时拥有 web.config 设置和全局EnableCors()属性 - 这会导致双打。
您似乎没有添加Access-Control-Allow-Credentials任何服务器端,但它可能是通过AllowCors 属性添加的,我不确定。(我自己偏向在OWIN中处理CORS)
| 归档时间: |
|
| 查看次数: |
21345 次 |
| 最近记录: |