vue怎么和python后端交互_新vue axios与python后端连接的程序
1.安裝axios環境與配置
npm install --save axios vue-axios
axios的配置
在入口main.js中導入axios 并將axios寫入vue的原型,這樣就能更簡單的使用
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
axios.get()方法
axios封裝了get方法,傳入請求地址和請求參數即可。 then中返回的是正常情況下的數據,而catch中返回的是錯誤信息。請求格式為: this.axios.get(url).then(res => {}).catch(err => {})
export default {
mounted: function () {
var url = 'http://127.0.0.1:8000/api/接口'
this.axios.get(url)
.then(res => {
console.log(res.data)
})
.catch(err => {
alert(err)
})
}
}
axios.post()方法
axios封裝了post方法,post方法可以接收請求參數,請求參數定義在格式為: this.axios.post(url, params).then(res => {}).catch(err => {})
export default {
mounted: function () {
var url = 'http://127.0.0.1:8000/api/接口/'
var params = {
title: '1213',
desc: '321'
}
this.axios.post(url, params)
.then(res => {
console.log(res.data)
})
.catch(err => {
alert(err)
})
}
}
axios經典寫法
axios的經典寫法,post請求格式為: this.axios({method: 'post',url: url,data: params}).then(res => {}).catch(err => {})
export default {
mounted: function () {
var url = 'http://127.0.0.1:8000/api/接口/'
var params = {
title: '121322',
desc: '321'
}
this.axios({
method: 'post',
url: url,
data: params
}).then(res => {
console.log('成功')
}).catch(err => {
console.log('失敗')
})
}
}
總結
以上是生活随笔為你收集整理的vue怎么和python后端交互_新vue axios与python后端连接的程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 文件与目录管理
- 下一篇: 土壤pH值传感器