1.环境安装
//nodejs环境 //npm install axios
2.axios发送get请求
const axios = require('axios') const url = "xxx.xxx.xxx"; const headers = {} axios.get(url, {headers}) .then(function (response) { console.log(response.data) res.json(response.data); }) .catch(function (error) { console.log(error); res.json(500); });
3.axios发送post请求
const axios = require('axios') const headers = {"Content-Type":"application/json"} const jsonBody = {"params":"body参数"} // 创建一个axios实例 const instance = axios.create({ baseURL: "xxx.xxx.xxx", // 设置请求的基本URL(IP或者域名) }); // 设置请求头(headers) instance.defaults.headers = headers; // 使用axios实例发送POST请求 instance.post('/path', jsonBody) .then(response => { console.log(response.data) res.json(response.data); }) .catch(error => { console.error(error); res.json(500); });
4.axios发送delete请求
const axios = require('axios') const headers = {"Content-Type":"application/json"} const jsonBody = {"params":"body参数"} onst instance = axios.create({ baseURL: "xxx.xxx.xxx", // 设置请求的基本URL }); // 设置请求头(headers) instance.defaults.headers = headers; // 使用axios实例发送delete请求 instance.delete('/delete/path', jsonBody) .then(response => { console.log(response.data) res.json(response.data); }) .catch(error => { console.error(error); res.json(500); });
5.待续....
标签:axios,const,get,xxx,headers,json,post,response From: https://www.cnblogs.com/xiaozhaoboke/p/17958253