使用提取时文本响应为空

Baz*_*Baz 2 javascript cors fetch-api

如下代码:

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)
        });
Run Code Online (Sandbox Code Playgroud)

输出:

Request successful 0
Run Code Online (Sandbox Code Playgroud)

如果我使用curl:

curl 'http://localhost:8080/root/1487171054127/k_query_bearer_token' \
  -H 'Cookie: JSESSIONID=CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf.ee16d0a01ad5' 
Run Code Online (Sandbox Code Playgroud)

我得到一个文本形式的令牌(长度!= 0)。

如果我通过以下方式输出响应标头:

curl 'http://localhost:8080/root/1487171054127/k_query_bearer_token' 
  -H 'Cookie: JSESSIONID=CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf.ee16d0a01ad5'
  --head
Run Code Online (Sandbox Code Playgroud)

我得到:

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
Run Code Online (Sandbox Code Playgroud)

为什么我无法通过提取获取任何文本?

sid*_*ker 5

删除mode: 'no-cors'

使用时,mode: 'no-cors'您明确指定要“不透明的响应”

您的脚本无法访问不透明响应的任何属性-实际上,您所能做的就是缓存它。mode: 'no-cors'仅在与Service Worker进行缓存时才有用。

如果您使用脚本的原因mode: 'no-cors'是因为对服务器的跨域请求否则将无法正常工作,则正确的解决方案是更新服务器端代码以发送Access-Control-Allow-Origin响应标头和其他CORS标头(如果您有权访问服务器可以做到这一点-否则,请使用反向代理(例如https://cors-anywhere.herokuapp.com/)