vue 添加axios解决post传参数为null问题

本文主要参考: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) {
相关文章
相关标签/搜索