React native react native中的fetch函数不起作用
我想用React native react native中的fetch函数不起作用,react-native,React Native,我想用fetch函数发送http请求。调用fetch后,没有响应,也没有错误。 我的问题在哪里 这是我的组件:(这是示例,我想在react native中测试http请求) const-App=()=>{ const changeTextHandler=异步文本=>{ 试一试{ const response=等待获取(“https://jsonplaceholder.typicode.com/todos/1"); const result=wait response.json(); consol
fetch
函数发送http请求。调用fetch
后,没有响应,也没有错误。
我的问题在哪里
这是我的组件:(这是示例,我想在react native中测试http请求)
const-App=()=>{
const changeTextHandler=异步文本=>{
试一试{
const response=等待获取(“https://jsonplaceholder.typicode.com/todos/1");
const result=wait response.json();
console.log(结果)
}
捕获(错误){
console.log(错误)
}
}
返回(
)
}
您的代码没有问题。jsonplaceholder API服务器可能由于某种原因而关闭。是否触发过onChangeText
?另外,您绝对不应该为onChangeText
事件调用fetch
,因为每次TextInput
组件/字段发生更改时,您都会重复触发onChangeText。是否触发过onChangeText?是的,这是您的权利,这是用于示例…我只想测试http请求这应该可以正常工作-可能请求需要更长的时间才能响应。如果您能够检查“网络”选项卡,那么我会检查请求是否被触发。@goto1-谢谢。我查看了网络选项卡,发现请求没有触发,为什么?
const App = () => {
const changeTextHandler = async text => {
try {
const response = await fetch("https://jsonplaceholder.typicode.com/todos/1");
const result = await response.json();
console.log(result)
}
catch (error) {
console.log(error)
}
}
return (
<View style={styles.container}>
<TextInput
onChangeText={changeTextHandler}
style={styles.textInput} />
</View>
)
}