【问题标题】:react-native axios.get() [Error: Request failed with status code 404]react-native axios.get() [错误:请求失败,状态码 404]
【发布时间】:2020-05-22 11:07:48
【问题描述】:

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

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

标头 - 订阅密钥:3009d4ccc29e4808af1ccc25c69b4d5d

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

我在 Postman Windows 上测试了这个链接。它返回给我一个 json 响应。但是当我使用相同的标头和路径变量在 react native 上尝试时,它返回了 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>
        )
    }
}

请帮忙!

【问题讨论】:

    标签: javascript node.js reactjs react-native axios


    【解决方案1】:

    你正在使用的兄弟 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
            });
    }

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 2020-09-18
      • 2019-10-12
      • 2019-11-27
      • 2020-11-15
      • 2019-09-29
      相关资源
      最近更新 更多