react axios的跨域请求
react axios的跨域请求1.安装npm install axios2.在package.json下添加“proxy”: {“/app”: {“target”: “http://open.douyucdn.cn”,“changeOrigin”: true}}3新增一个http.js文件import axios from ‘axios’;axios.defaults.ba...
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(
)
}
}
export default Footer;
更多推荐
所有评论(0)