欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

通过vue实现添加axios组件,解决post传参数为null方面的问题(详细教程)

程序员文章站 2022-03-28 09:31:10
...
下面我就为大家分享一篇基于vue 添加axios组件,解决post传参数为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、运行结果:

通过vue实现添加axios组件,解决post传参数为null方面的问题(详细教程)

通过vue实现添加axios组件,解决post传参数为null方面的问题(详细教程)

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) {

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

聊聊JS动画库 Velocity.js的使用

vue 过滤器filter实例详解

Vue.js 动态为img的src赋值方法

以上就是通过vue实现添加axios组件,解决post传参数为null方面的问题(详细教程)的详细内容,更多请关注其它相关文章!