admin管理员组文章数量:1025278
I have two fetch
scripts that work great at either or though I can't figure out how to bine them.
This first one allows me to know what the response.status
is however even though it somehow knows the server's HTTP response while not having the response body (yeah, asynchronous):
fetch(url).then(function(r)
{
if (r.status != 200) {alert('Error: unable to load preview, HTTP response '+r.status+'.');}
else
{
console.log(r.text());//Promise { <state>: "pending" }, no good.
}
}).catch(function(err) {alert('Error: '+err);});
This second script allows me to access the response.text()
though I have no access to the response.status
:
fetch(url).then(r => r.text()).then(function(r)
{
console.log(r);//response text.
});
How do I bine the scripts properly so I have access to both the response.status
and response.text()
after the request has been received?
I have two fetch
scripts that work great at either or though I can't figure out how to bine them.
This first one allows me to know what the response.status
is however even though it somehow knows the server's HTTP response while not having the response body (yeah, asynchronous):
fetch(url).then(function(r)
{
if (r.status != 200) {alert('Error: unable to load preview, HTTP response '+r.status+'.');}
else
{
console.log(r.text());//Promise { <state>: "pending" }, no good.
}
}).catch(function(err) {alert('Error: '+err);});
This second script allows me to access the response.text()
though I have no access to the response.status
:
fetch(url).then(r => r.text()).then(function(r)
{
console.log(r);//response text.
});
How do I bine the scripts properly so I have access to both the response.status
and response.text()
after the request has been received?
- You can create a funciton at first then handler to do more than just returning r.text() – Shubham Srivastava Commented Dec 16, 2020 at 6:57
- @ShubhamSrivastava Could you post please? If it works I'd be happy to accept/vote. – John Commented Dec 16, 2020 at 6:58
-
Whoa, I didn't expect three answers! I'll properly go through everything later tomorrow. Thank you for the help!
I have two
fetch
scripts that work great at either or though I can't figure out how to bine them.This first one allows me to know what the
response.status
is however even though it somehow knows the server's HTTP response while not having the response body (yeah, asynchronous):fetch(url).then(function(r) { if (r.status != 200) {alert('Error: unable to load preview, HTTP response '+r.status+'.');} else { console.log(r.text());//Promise { <state>: "pending" }, no good. } }).catch(function(err) {alert('Error: '+err);});
This second script allows me to access the
response.text()
though I have no access to theresponse.status
:fetch(url).then(r => r.text()).then(function(r) { console.log(r);//response text. });
How do I bine the scripts properly so I have access to both the
response.status
andresponse.text()
after the request has been received?I have two
fetch
scripts that work great at either or though I can't figure out how to bine them.This first one allows me to know what the
response.status
is however even though it somehow knows the server's HTTP response while not having the response body (yeah, asynchronous):fetch(url).then(function(r) { if (r.status != 200) {alert('Error: unable to load preview, HTTP response '+r.status+'.');} else { console.log(r.text());//Promise { <state>: "pending" }, no good. } }).catch(function(err) {alert('Error: '+err);});
This second script allows me to access the
response.text()
though I have no access to theresponse.status
:fetch(url).then(r => r.text()).then(function(r) { console.log(r);//response text. });
How do I bine the scripts properly so I have access to both the
Share Improve this question asked Dec 16, 2020 at 6:55 JohnJohn 13.8k15 gold badges111 silver badges192 bronze badges 3response.status
andresponse.text()
after the request has been received?- You can create a funciton at first then handler to do more than just returning r.text() – Shubham Srivastava Commented Dec 16, 2020 at 6:57
- @ShubhamSrivastava Could you post please? If it works I'd be happy to accept/vote. – John Commented Dec 16, 2020 at 6:58
-
Whoa, I didn't expect three answers! I'll properly go through everything later tomorrow. Thank you for the help!
本文标签: JavaScript fetch responsestatus or responsetext() though not bothStack Overflow
版权声明:本文标题:JavaScript fetch response.status or response.text() though not both - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745620588a2159549.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论