本文主要參考:html
https://www.npmjs.com/package/axiosios
http://jingyan.baidu.com/article/29697b916d6a7bab20de3cf9.htmlajax
好,下面上貨。npm
一、安裝axiosjson
npm install axios --saveaxios
二、添加axios組件app
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;
三、get請求post
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); }); },
四、post請求this
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); }) }
五、運行結果:url
六、注意:
在使用post方式的時候傳遞參數有兩種方式,一種是普通方式,一種是json方式,若是後臺接受的是普通方式,那麼使用上述方式便可。
普通的formed方式
var params = new URLSearchParams(); params.append('name', 'hello jdmc你好'); params.append('id', '2');
data:params
後臺接收參數:
public Student greeting2(int id,String name) {
json方式
data: {id: '3', name: 'abc'}
後臺接收參數
public Object greeting2(@RequestBody Object student) {