【发布时间】:2014-03-18 00:15:23
【问题描述】:
我无法使用 Authorization 标头发出跨域请求(使用 Firefox 进行测试)。我的请求无需身份验证即可工作,但一旦我将 withCredentials 设置为 true,我就无法再读取来自服务器的响应。
在服务器上,我发回这些标头(使用 Flask 中的 after_request 方法):
resp.headers['Access-Control-Allow-Origin'] = '*'
resp.headers['Access-Control-Allow-Credentials'] = 'true'
resp.headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
resp.headers['Access-Control-Allow-Headers'] = 'Authorization'
Firefox 从未实际调用过任何 OPTIONS。在客户端我进行 XMLHttpRequest 调用:
var xhr = new XMLHttpRequest()
xhr.open( 'POST', 'http://test.local:8002/test/upload', true)
xhr.withCredentials = true
xhr.onreadystatechange = function() {
console.log( xhr.status, xhr.statusText )
}
xhr.send(fd)
如果没有withCredentials 设置,日志语句会将预期信息记录到控制台。一旦我设置了值,但是 xhr 不允许访问,我只写一个 0 值和一个空字符串。我这里没有设置授权标头,但这应该不会影响我读取结果的能力。
如果我尝试将用户名/密码添加到“打开”命令,我会收到 NS_ERROR_DOM_BAD_URI: Access to restricted URI denied 错误。
我做错了什么?
【问题讨论】:
标签: http xmlhttprequest cors