react axios的跨域请求
1.安装npm install axios

2.在package.json下添加

“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(

axios封装

)
}
}

export default Footer;

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐