英文:
React native Expo project, Fetch the API will get [TypeError: Network request failed], but the others api can work
问题
When I fetch the API in React Native Expo project, I receive the following error:
> [TypeError: Network request failed].
I have tried fetching other APIs, and they are successful.
I also attempted to resolve the issue using this question, but it didn't work for me.
(Also tried this https://stackoverflow.com/questions/38418998/react-native-fetch-network-request-failed)
Is the problem related to the API?
My code looks like this:
const val = { uId: 'molly', pwd: '123', id: '1' }
const url = 'https://c........com.tw:8899'
let header = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(val),
}
fetch(`${url}/auth/login`, header)
.then((r) => r.json())
.then((d) => {
if (d.code === 0) {
console.log(d.data)
} else {
console.log('MSG', d.msg)
}
})
.catch((err) => console.log(err))
I have also tried the following, and it works (so it's not a network problem):
fetch('https://randomuser.me/api/')
.then((r) => r.json())
.then((d) => {
console.log('ok')
})
.catch((err) => console.log(err))
英文:
When I fetch the API in React native Expo project will get:
> [TypeError: Network request failed].
I have try to fetch other API, it can get data success.
And also try to fix by this question but didn't work for me.
(Also tried this https://stackoverflow.com/questions/38418998/react-native-fetch-network-request-failed)
Is API's problem?
My code like:
const val = { uId: 'molly', pwd: '123', id: '1' }
const url = 'https://c........com.tw:8899'
let header = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(val),
}
fetch(`${url}/auth/login`, header)
.then((r) => r.json())
.then((d) => {
if (d.code === 0) {
console.log(d.data)
} else {
console.log('MSG', d.msg)
}
})
.catch((err) => console.log(err))
And I have try this, it can work. (So is not the network problem.)
fetch('https://randomuser.me/api/')
.then((r) => r.json())
.then((d) => {
console.log('ok')
})
.catch((err) => console.log(err))
答案1
得分: 0
我找到问题是关于SSL的。
应该使用这个包来处理
react-native-ssl-pinning
https://www.npmjs.com/package/react-native-ssl-pinning
英文:
I found the problem is about ssl.
Should use this package to deal with
react-native-ssl-pinning
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论