Axios 错误:在 React Native 中使用 Axios get 方法时出现网络错误

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

我在

Axios
中使用
react native
作为网络提供商,当我尝试实现在 iOS 和 Android 中随机获得
network error 
的 api 时,我使用默认检查器来调试问题,但我不这样做认为这有什么错误,但我遇到了网络错误,任何人都可以帮助我解决这个问题。

here is my code

import axios from 'axios';
import { API_KEY, BASE_URL } from '../constants';

const fetchConfigAPI = async () => {
    try {
        const response = await axios.get(`${BASE_URL}/config`, {
            headers: {
                Accept: 'application/json',
                'x-tutor-api-key': API_KEY,
            },
        });
        return response;
    } catch (error) {
        console.log('Error fetching config API', error);
        throw error;
    }
};

export default fetchConfigAPI;

android ios react-native axios
1个回答
0
投票

试试这个

const fetchConfigAPI = async () => {
try {
    const response = await axios.get(`${BASE_URL}/config`, {
        headers: {
            'Content-Type': 'application/json',
            'x-tutor-api-key': API_KEY,
        },
    })
    if (response.status === 200) {
        return response.data
    } else {
        throw new Error(`HTTP Error: ${response.status}`)
    }
} catch (error) {
    console.error('Error fetching config API:', error)
    throw error
}

}

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