使用 fetch() 获取 txt 文件时出错
Error when getting txt file using fetch()
我想获取 this page 的内容,并将其存储在一个变量中。
我有以下代码:
// fetch the txt file
const fileUrl = 'https://pastebin.pl/view/raw/2facc037'
fetch(fileUrl)
.then( r => r.text() )
.then( t => {var message = t} )
console.log(message)
<!DOCTYPE html>
<head>
<title>Test Document</title>
</head>
<body>
<h1>Open developer tools...</h1>
</body>
</html>
报错。为什么?
PS:我有充分的理由将其存储为一个变量
您仅在最后一个 then
回调的范围内声明 message
变量。将控制台日志移至此处以记录消息:
// fetch the txt file
const fileUrl = 'https://pastebin.pl/view/raw/2facc037'
fetch(fileUrl)
.then( r => r.text() )
.then( t => {
var message = t;
console.log(message);
} )
我想获取 this page 的内容,并将其存储在一个变量中。
我有以下代码:
// fetch the txt file
const fileUrl = 'https://pastebin.pl/view/raw/2facc037'
fetch(fileUrl)
.then( r => r.text() )
.then( t => {var message = t} )
console.log(message)
<!DOCTYPE html>
<head>
<title>Test Document</title>
</head>
<body>
<h1>Open developer tools...</h1>
</body>
</html>
报错。为什么?
PS:我有充分的理由将其存储为一个变量
您仅在最后一个 then
回调的范围内声明 message
变量。将控制台日志移至此处以记录消息:
// fetch the txt file
const fileUrl = 'https://pastebin.pl/view/raw/2facc037'
fetch(fileUrl)
.then( r => r.text() )
.then( t => {
var message = t;
console.log(message);
} )