javascript - 使用 fetch() 时继续得到“未处理的承诺拒绝:TypeError:网络请求失败”
问题描述
我正在尝试从项目中的文本文件中获取数据并将其打印出来作为测试,但我仍然遇到同样的错误。
下面是我的代码:
import React from 'react';
import { Stylesheet, View, Text, Button } from 'react-native';
import { beginQuiz } from './helperfunctions';
import myText from './sampleFile.js';
export const QuestionScreen = ({ navigation, route }) => {
fetch(myText)
.then(response => response.text())
.then(data => console.log(data))
.catch(function(error) {
console.log('There has been an error');
throw error;
})
console.log('done');
return (
<View>
<Text>This is the question screen</Text>
<Button title='Sample Button'/>
</View>
)
};
这是 sampleFile.js:
var myText = 'This is a sample text file.'
解决方案
推荐阅读
- python - Python 的 Kivy 屏幕不存在
- installation - 如何连接动态 AX 2009 和 Visual Studio 2008
- javascript - OnChange function doesn't trigger when change occurs
- reactjs - 如何根据 saga 处理程序的响应在屏幕上显示消息
- windows - 如何通过 PowerShell 脚本将文件从 Windows 复制到 Ubuntu
- c# - 如何让 Nswag 生成的 api 客户端停止产生对象引用错误?
- javascript - 无法构建 React 应用程序 - 管道惨遭失败
- reactjs - 模拟输入变化不调用函数
- git - Github 忽略所有但包含一个文件夹
- audio - 用于无损操作(例如剪切、淡入淡出)的 DAW 音频插件开发