码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2018-09-13 20:00:25      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:lse   www.   https   rom   err   term   var   form   pack   

本文主要参考:

https://www.npmjs.com/package/axios

http://jingyan.baidu.com/article/29697b916d6a7bab20de3cf9.html

 

好,下面上货。

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、运行结果:

技术分享图片

 

 

技术分享图片

 

6、注意:

在使用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) {

 

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

标签:lse   www.   https   rom   err   term   var   form   pack   

原文地址:https://www.cnblogs.com/zhaowy/p/9642185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!