react中如何发送ajax请求呢?
想要发送请求首先当然是安装相关依赖(axios)啦!
如果你使用node安装,指令如下
1 npm install axios
如果使用yarn安装,指令如下:
yarn add axios
请求方式:
//get方式:
axios.get(url)
.then(response=>{
})
.catch(error =>{
console.log(error.message)
})
//post方式
axios.post(url,{参数对象})
.then(response=>{
console.log(response)
})
.catch(error =>{
console.log(error.message)
})
使用fetch发生请求:
fetch(url).then(function(response){
return response.json()
}).then(function(data){
console.log(data);//这才是返回的数据
}).catch(function(e){
console.log(e);
})