首页 > 解决方案 > react-native axios.get() [错误:请求失败,状态码 404]

问题描述

我正在尝试从此 url ( https://documenter.getpostman.com/view/8854915/SzS7R74q?version=latest ) 实现一个 api。

API 链接- https://api.smartable.ai/coronavirus/news/:location

标头- 订阅密钥:3009d4ccc29e4808af1ccc25c69b4d5d

路径变量- 位置:美国(示例)

我在 Postman Windows 上测试了这个链接。它返回给我一个 json 响应。但是当我尝试使用相同的标头和路径变量对本机反应时,它返回了 404 错误。

import {
    SafeAreaView,
    StyleSheet,
    ScrollView,
    View,
    Text,
    StatusBar,
} from 'react-native';

import axios from 'axios';


export default class NationalNews extends React.Component {

    state = {
        data: ''
    }

    componentDidMount() {
        axios.get('https://api.smartable.ai/coronavirus/news/:location', {
            params: {
                "location": "US"   //it maybe just IN
            },
            headers: {
                'Subscription-Key': '429058f92b3246ae9b8ad1cd88daee46',
            },
            method: 'get'

        })
            .then(function (response) {
                // handle success
                console.log(response);
            })
            .catch(function (error) {
                // handle error
                console.log(error);
            })
            .then(function () {
                // always executed
            });
    }


    render() {

        return (
            <View>
                <Text></Text>
            </View>
        )
    }
}

请帮忙!

标签: javascriptnode.jsreactjsreact-nativeaxios

解决方案


您正在使用的兄弟 axios 语法不起作用尝试此代码工作正常

  componentDidMount() {
    axios.get('https://api.smartable.ai/coronavirus/news/US', {
        headers: {
            'Subscription-Key': '429058f92b3246ae9b8ad1cd88daee46',
        },
    })
        .then(function (response) {
            // handle success
            console.log(response);
        })
        .catch(function (error) {
            // handle error
            console.log(error);
        })
        .then(function () {
            // always executed
        });
}


推荐阅读