React native Expo project, Fetch the API will get [TypeError: Network request failed], but the others api can work

huangapple go评论70阅读模式
英文:

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

https://www.npmjs.com/package/react-native-ssl-pinning

huangapple
  • 本文由 发表于 2023年1月6日 12:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定