Vue前端如何實現(xiàn)與后端進行數(shù)據(jù)交互
更新時間:2022年03月29日 14:37:30 作者:聽聽那晚風(fēng)
這篇文章主要介紹了Vue前端如何實現(xiàn)與后端進行數(shù)據(jù)交互,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
vue前端與后端數(shù)據(jù)交互
安裝
npm install axios --save
在main.js文件引入
import Axios from 'axios';//后臺交互 Vue.prototype.$http=Axios //defaults 設(shè)置全局默認路徑 Axios.defaults.baseURL="/"
使用
//第一種
this.$http.post('/index/customer/',{//里面寫要傳的值}).then(function(res) {
?? ?// console.log('這是返回的客戶數(shù)據(jù)');
?? ?// console.log(res.data.data);
?? ?this.customerArr = res.data.data
});//如果是get請求就把post換成get
//第二種,推薦使用該方法
this.$http({
?? ??? ??? ??? ?url: '/index/patchBase/',
?? ??? ??? ??? ?method: 'get',
?? ??? ??? ??? ?headers: { 'X-Requested-With': 'XMLHttpRequest' },
?? ??? ??? ??? ?params:{}//傳值 如:params:{id:1,name:"gw"}
?? ??? ??? ?}).then(res => {
?? ??? ??? ??? ?console.log('數(shù)據(jù)接收');
?? ??? ??? ??? ?console.log(res.data.data);
?? ??? ??? ?});
this.$http({
?? ??? ??? ??? ?url: '/index/patchBase/',
?? ??? ??? ??? ?method: 'post',
?? ??? ??? ??? ?headers: { "Content-Type": "multipart/form-data" },
?? ??? ??? ??? ?data:{}//傳值
?? ??? ??? ?}).then(res => {
?? ??? ??? ??? ?console.log('數(shù)據(jù)接收');
?? ??? ??? ??? ?console.log(res.data.data);
?? ??? ??? ?});需要注意的是 post、get請求的請求頭數(shù)據(jù)不一樣,傳值方法不一樣:get是params,post請求是用data傳值
啟動vue和前后端連接
直接上圖














以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue清除瀏覽器全部cookie的問題及解決方法(絕對有效!)
最近項目要實現(xiàn)關(guān)閉瀏覽器清除用戶緩存的功能,下面這篇文章主要給大家介紹了關(guān)于vue清除瀏覽器全部cookie的問題及解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06
Vuex中如何getters動態(tài)獲取state的值
這篇文章主要介紹了Vuex中如何getters動態(tài)獲取state的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

