【发布时间】:2021-08-07 06:07:11
【问题描述】:
我现在尝试了 3 个不同的项目来使 fetch 或 axios 工作,但我不能。我尝试过更改 android 清单,就像某些人在某些问题上尝试过的那样,但它不起作用。不是等待/异步,不是承诺,甚至不是这个超级简单的例子。甚至都没有调用 finally。
它不返回错误、日志或任何东西,什么也没有发生。
经过进一步测试,看起来 Mac/IOS 可以正常工作,但 Windows/Android 不行。
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useEffect} from 'react';
import {SafeAreaView, Text} from 'react-native';
const App = () => {
useEffect(() => {
console.log('hola');
fetch('http://jsonplaceholder.typicode.com/albums')
.then(response => {
console.log('Hola');
console.log(response.json());
})
.then(json => console.log(json))
.catch(error => console.error(error))
.finally(() => console.log('Finally'));
}, []);
return (
<SafeAreaView>
<Text>Hola</Text>
</SafeAreaView>
);
};
export default App;
这是 package.json:
{
"name": "prueba",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"axios": "^0.21.1",
"react": "17.0.1",
"react-native": "0.64.1"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/runtime": "^7.12.5",
"@react-native-community/eslint-config": "^2.0.0",
"babel-jest": "^26.6.3",
"eslint": "7.14.0",
"jest": "^26.6.3",
"metro-react-native-babel-preset": "^0.64.0",
"react-test-renderer": "17.0.1"
},
"jest": {
"preset": "react-native"
}
}
【问题讨论】:
-
您是否尝试过调用安全 URL,即
https://jsonplaceholder.typicode.com/albums。我有一个模糊的记忆,不允许不安全的提取 -
@Phil 是的,我做了同样的事情。
-
您在控制台中看到了哪些
console.log()调用?仅供参考,您的第一个.then回调应该return response.json()而不是记录它 -
@Phil 我看到的唯一控制台日志是“hola”,获取后什么也没有
-
经过进一步测试,Mac/IOS 好像可以,但是 Windows/Android 不行
标签: javascript typescript react-native axios fetch