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);
})