当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 基于vue 添加axios组件,解决post传参数为null的问题

基于vue 添加axios组件,解决post传参数为null的问题

2019年06月06日  | 移动技术网IT编程  | 我要评论

龟山岩绿,石家庄动感地带,我的王子爱东施

好,下面上货。

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的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网