1.安裝npm install axiosreact
2.在package.json下添加ios
"proxy": {
"/app": {
"target": "http://open.douyucdn.cn",
"changeOrigin": true
}
}
3新增一個http.js文件
import axios from 'axios';
axios.defaults.baseURL = 'http://open.douyucdn.cn';
axios.defaults.withCredentials = true;
axios.defaults.timeout = 100000;
let http = {
post:'',
get:''
};
http.post = function (api ,data) {
let params =JSON.stringify(data);
return new Promise((resolve,reject) =>{
axios.post(api, params).then(res=>{
resolve(res);
})
})
};
http.get = function (api ,data) {
let params =JSON.stringify(data);
return new Promise((resolve,reject) =>{
axios.get(api, params).then(res=>{
resolve(res);
})
})
};
export default http;
4.進行數據請求
import React,{Component} from 'react';import http from '../../libs/http'class Footer extends React.Component{ async getStudentList(){ const res = await http.post('/app/mobile/login/',{name:'boonook',passworld:'123456'}); console.log(res); } componentDidMount(){ this.getStudentList(); } render(){ return( <div>axios封裝</div> ) }}export default Footer;