Axios API 请求

Node.js Axios API 请求

安装

npm install axios
yarn add axios

Axios 跨网域 CORS 请求

前端框架

import axios from 'axios';

let data = {
    key: 'value'
};

try {
    let response = await axios.post('https://localhost:3000/api', data, {
        httpsAgent: {
            rejectUnauthorized: false
        },
        validateStatus: function (status) {
            return true; // always resolve
        }
    });

    console.log(response.data);
} catch (error) {
    console.log(error);
}

Node.js

import axios from 'axios';
import https from 'https';

let data = {
    key: 'value'
};

try {
    let response = await axios.post('https://localhost:3000/api', data, {
        httpsAgent: new https.Agent({
            rejectUnauthorized: false
        }),
        validateStatus: function (status) {
            return true; // always resolve
        }
    });

    console.log(response.data);
} catch (error) {
    console.log(error);
}

参考资料