IOS 上的 Expo Go 应用程序:axios api 调用出现网络故障

问题描述 投票:0回答:1

我正在使用 expo go 在我的 iPhone 上运行我的 React Native 应用程序。我有一个托管在渲染上的后端服务来处理我的 api 调用。我正在尝试使用 axios 来调用此服务。下面是我为处理整个应用程序中的 api 调用而制作的自定义类。我可以登录并访问某些端点,但某些端点只是显示:[AxiosError:网络错误]。当发生这种情况时,它不会到达我的 api,但我仍然能够在邮递员中到达该端点。如果我在启动应用程序时使用“开放网络”选项运行应用程序,它也可以工作。

世博会指挥:

npx expo start --tunnel

定制类:

import axios from 'axios';

class ApiService{

    constructor() {
        this.baseUrl =  process.env.API_URL;
        this.axios = axios.create({
            baseURL: this.baseUrl,
        });
    }

    async call(method, url, data) {
        const headers = {
        "Content-Type": "application/json",
        };

        try {
            let response = await this.axios({ method, url, data, headers });

            return response.data;
        } catch (err) {
            return err
        }
    }
}

const apiService = new ApiService();

export default apiService;

调用示例(错误):

const res = (await apiService.call('get', `student/${student_id}/sports`)).data || [];

调用示例(成功):

const res= (await axios.get(process.env.API_URL + `student/${student_id}/sports`, {
    headers: {
        "Content-Type": "application/json",
    }
})).data?.data || [];

Package json(相关的包):

"axios": "^1.6.8",
"expo": "~50.0.11",
"react-native": "0.73.6",
"react": "18.2.0",
"react-native-dotenv": "^3.4.11",
"@expo/metro-runtime": "~3.1.3",

大约一周前它就开始工作了,因为我能够在其他地方到达端点,所以看起来这不是我的网址或后端的问题。我认为这与世博会方面的事情有关。我尝试将 appTransportSecurity 添加到我的 app.json 并删除节点模块,确保包兼容并重新安装,但这没有用。

react-native axios expo expo-go general-network-error
1个回答
0
投票

我也遇到过类似的问题。问题是端点的 SSL 已过期或无效。发生这种情况的另一个原因可能是端点中的http。 IOS不支持http,请后端团队将其设为https(基本确保端点安全)。

© www.soinside.com 2019 - 2024. All rights reserved.