1、axios的get請求方式:
axios.get("armour",{//get后的第一項參數為請求的url params:{//請求參數 "aname":aname//請求參數名:請求參數值
"id":id
} }).then(response=> (this.info=response.data)) .catch(function (error) { console.log(error); }) } } }) </script>
get請求也可以使用在url后面直接拼請求參數的方式發送請求參數,這樣就可以直接省略get()的第二項參數了。
2、axios的post請求方式:
axios.post("armour","aname="+name)//post后的第一項參數為請求url,第二項參數為請求參數,多對請求參數用‘&’連接
.then(response=>(this.info=response.data)) .catch(function (error) { console.log(error); })
3、在菜鳥教程上給的post請求方式是下面的代碼:
axios.post('/user', { firstName: 'Fred', // 參數 firstName lastName: 'Flintstone' // 參數 lastName }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
博主在按照菜鳥教程上給的代碼敲了之后發現后台無法獲取請求參數,后來看了一個大佬的博文才知道:
按照菜鳥教程上給的方式寫請求參數的話,請求參數是以json的形式發送的,但目前博主所使用的后端解析請求參數的方式都只能解析類似“id=1&name=zhagnsan”這樣的請求參數,無法解析json格式的請求參數。
博主的第二段代碼是axios以常規方式發送post請求參數的最簡單的一種寫法。
參考博文: