当我在react-native和redux中使用fetch时,fetch不能运行。但是catch可以运行。如果有错误,它可以警告我想要的错误。
我的网址不是本地主机,可以访问。
我在浏览器控制台中使用相同的提取请求,它可以正常工作。
我尝试使用whatwg-fetch,但当时仍无法运行。
在我的提取API中:
export const getApi= (url: string, query: string, variables: any = null) => {
return new Promise((resolve: any, reject: any) => {
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify({
"query": `${query}`,
"variables": `${variables}`
})
})
.then((r: any) => r.json())
.then((res: { errors: any, data: any }) => {
if (res.errors) { reject(res.errors); return }
resolve(res.data)
})
.catch((e: any) => { alert('network error'); });
})
}
此后的逻辑无法执行,我尝试在该控制台中运行,但无法正常工作。为什么然后无法运行,我不知道。我当时使用了反应性调试来进行控制台,但没有任何障碍。如果获取失败,则捕获将被覆盖,警报可以运行。
我的package.json
"dependencies": {
"@types/react-redux": "^7.1.0",
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-graphql": "^0.7.1",
"mysql": "^2.17.1",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-native": "^0.59.9",
"react-native-gesture-handler": "^1.3.0",
"react-navigation": "^3.11.0",
"react-redux": "^7.1.0",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0",
"whatwg-fetch": "^3.0.0"
},
"devDependencies": {
"@types/react": "^16.8.21",
"@types/react-native": "^0.57.63",
"@types/react-navigation": "^3.0.7",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "24.1.0",
"babel-preset-react": "^6.24.1",
"eslint": "^4.19.1",
"eslint-plugin-graphql": "^3.0.3",
"eslint-plugin-react": "^7.13.0",
"eslint-plugin-react-native": "^3.7.0",
"eslint-plugin-typescript": "^0.14.0",
"graphql": "^14.3.1",
"jest": "24.1.0",
"metro-react-native-babel-preset": "0.52.0",
"react-native-typescript-transformer": "^1.2.12",
"react-test-renderer": "16.6.3",
"typescript": "^3.5.2",
"typescript-eslint-parser": "^22.0.0"
},