基于vue 添加axios組件,解決post傳參數(shù)為null的問題
更新時間:2018年03月05日 15:16:43 作者:0day__
下面小編就為大家分享一篇基于vue 添加axios組件,解決post傳參數(shù)為null的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
好,下面上貨。
1、安裝axios
npm install axios --save
2、添加axios組件
import axios from 'axios' axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; axios.defaults.baseURL = 'http://localhost:7878/zkview'; Vue.prototype.$ajax = axios;
3、get請求
testGet: function () { this.$ajax({ method: 'get', url: '/test/greeting', params: { firstName: 'Fred', lastName: 'Flintstone' } }).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); }); },
4、post請求
testPost: function () { var params = new URLSearchParams(); params.append('name', 'hello jdmc你好'); params.append('id', '2'); this.$ajax({ method: 'post', url: '/test/greeting2', data:params // data: {id: '3', name: 'abc'} }).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); }) }
5、運行結(jié)果:
6、注意:
在使用post方式的時候傳遞參數(shù)有兩種方式,一種是普通方式,一種是json方式,如果后臺接受的是普通方式,那么使用上述方式即可。
普通的formed方式
var params = new URLSearchParams(); params.append('name', 'hello jdmc你好'); params.append('id', '2'); data:params
后臺接收參數(shù):
public Student greeting2(int id,String name) {
json方式
data: {id: '3', name: 'abc'}
后臺接收參數(shù)
public Object greeting2(@RequestBody Object student) {
以上這篇基于vue 添加axios組件,解決post傳參數(shù)為null的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue-router的使用和出現(xiàn)空白頁,路由對象屬性詳解
今天小編就為大家分享一篇Vue-router的使用和出現(xiàn)空白頁,路由對象屬性詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue單向以及雙向數(shù)據(jù)綁定方式(v-bind和v-model的使用)
這篇文章主要介紹了vue單向以及雙向數(shù)據(jù)綁定方式(v-bind和v-model的使用),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04