以下代码:
fetch('http://localhost:8080/root/1487171054127/k_query_bearer_token', {
mode: 'no-cors', credentials: 'include'
})
.then(function (response) {
return response.text();
})
.then(function (text) {
console.log('Request successful', text.length);
})
.catch(function (error) {
log('Request failed', error)
});
正在输出:
Request successful 0
如果我使用卷曲:
curl 'http://localhost:8080/root/1487171054127/k_query_bearer_token' \
-H 'Cookie: JSESSIONID=CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf.ee16d0a01ad5'
我收到一个文本形式的令牌(长度!= 0)。
如果我通过以下方式输出响应标头:
curl 'http://localhost:8080/root/1487171054127/k_query_bearer_token'
-H 'Cookie: JSESSIONID=CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf.ee16d0a01ad5'
--head
I get:
HTTP/1.1 200 OK
Connection: keep-alive
X-Powered-By: Undertow/1
Server: JBoss-EAP/7
Content-Type: text/plain
Content-Length: 1730
Date: Wed, 15 Feb 2017 16:17:00 GMT
为什么我通过 fetch 没有收到任何文本?
Remove mode: 'no-cors'
.
当你使用no-cors
模式,您明确指定您想要一个“反应不透明”.
您的脚本无法访问不透明响应的任何属性 - 实际上您所能做的就是缓存它。no-cors
模式基本上仅在使用 Service Workers 进行缓存时才有用。
如果您的脚本使用的原因是no-cors
模式是因为对服务器的跨域请求否则将无法工作,正确的解决方案是更新服务器端代码以发送Access-Control-Allow-Origin
响应标头和其他 CORS 标头(如果您有权访问服务器,请执行此操作),否则,请使用代理,例如https://cors-anywhere.herokuapp.com/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)