使用提取时文本响应为空 [英] Text response is empty when using fetch

查看:80
本文介绍了使用提取时文本响应为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

  fetch('http:// localhost:8080 / root / 1487171054127 / k_query_bearer_token',{ 
模式:'no-cors',凭据:'include'
})
.then(function(response){
return response.text();
}}
.then(函数(文本){
console.log('请求成功',text.length);
})
.catch(函数(错误){
log('Request failed',error)
});

正在输出:

 请求成功0 

如果我使用curl:

  curl'http:// localhost:8080 / root / 1487171054127 / k_query_bearer_token'\ 
-H'Cookie:JSESSIONID = CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf。 ee16d0a01ad5'

我以文本形式返回令牌(长度!= 0)。



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

  curl'http://本地主机:8080 / root / 1487171054127 / k_query_bearer_token'
-H'Cookie:JSESSIONID = CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf.ee16d0a01ad5'
--head $ b $ c> $

我得到:

  HTTP / 1.1 200 OK 
连接:keep-alive
X-Powered-By:Undertow / 1
服务器:JBoss-EAP / 7
内容类型:text / plain
内容长度:1730
日期:2017年2月15日,星期三16:17:00 GMT

为什么我没有收到任何文字通过获取?

解决方案

删除模式:'no-cors'



当您使用模式:'no-cors'时,您明确指定要使用 stackoverflow.com/questions/36292537/what-is-an-opaque-request-and-what-it-serves-for>不透明的响应



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



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


The following code:

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)
        });

is outputting:

Request successful 0

If I use curl:

curl 'http://localhost:8080/root/1487171054127/k_query_bearer_token' \
  -H 'Cookie: JSESSIONID=CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf.ee16d0a01ad5' 

I get a token in text form back (length != 0).

And if I output the response header via:

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

Why am I getting no text via fetch?

解决方案

Remove mode: 'no-cors'.

When you use mode: 'no-cors' you’re explicitly specifying that you want an "opaque response".

Your script can’t access any properties of an opaque response—instead essentially all you can do is cache it. mode: 'no-cors' is basically useful only when doing caching with Service Workers.

If the reason you have your script using mode: 'no-cors' is because cross-origin requests to the server otherwise won’t work, the right solution is either to update the server-side code to send the Access-Control-Allow-Origin response header and other CORS headers—if you have access to the server do to that—or else, use a reverse proxy like https://cors-anywhere.herokuapp.com/.

这篇关于使用提取时文本响应为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆