【发布时间】:2014-10-21 20:30:23
【问题描述】:
我正在尝试将 CORS 支持添加到我的 API。我添加了 nuget 包 Microsoft.AspNet.WebApi.Cors。我已将此添加到我的 WebgApiConfig.cs 文件中,在注册中
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
我也尝试在控制器上进行配置。无论我尝试什么,Access-Control-Allow-Origin 标头都不会添加到响应中,当我执行 javascript 时,我会在 Chrome 中收到此消息
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:19560' is therefore not allowed access. The response had HTTP status code 401
对我可能遗漏的东西有什么想法吗?
如果我设置 ajax contentType 现在它可以工作了 contentType: '文本/纯文本'
显然这是需要的,因为没有路由处理来自浏览器的预检选项请求。现在我需要弄清楚如何处理预检选项检查,这样我就不必设置 contentType。
【问题讨论】:
-
我发现了我的问题。在我的公司网络上,我们使用 Kerberos 身份验证。我需要添加 jQuery ajax 选项
xhrFields: {withCredentials:true}
标签: asp.net cors asp.net-web-api2