vue结合axios传递参数

首先要安装好axios

import axios from "axios";  //导入axios
export default(args){
  testInterface(args){   //接口
      return axios.post("test1/test1/test1",args);
  },
  testInterface(args){
      return axios.post("test2/test2/test2",args);
  } 
};

把form表单转Json格式

function fromJson(json){
    return isString(json) ? JSON.parse(json) : json;
}

vue中方法

methods:{
    testFun1(){
        var args = this.q.orgs.value;
        cfg.testInterface(fromJson(args)).then(res=>{
            if(res){
                this.$message({
                    type:"success",
                    message:"成功"
                });
            }else{
                this.$message({
                    type:"warning",
                    message:"失败"
                });
            }
        });
    },
    testFun2(){
        var args = {
            str:this.q.orgs.value
        };
        cfg.testInterface(args).then(res=>{
            if(res){
                this.$message({
                    type:"success",
                    message:"成功"
                });
            }else{
                this.$message({
                    type:"warning",
                    message:"失败"
                });
            }
        });
    },
}

Controller层

@RequestMapping("test1")
public int test(@RequestBody JSONObject args){
    try{
        testService.testInfo(args.toJSONString());
        return 1;
    }catch(Exception e){
        return 0;
    }
}

@RequestMapping("test2")
public int test(@RequestBody String args){
    try{
        testService.testInfo(args);
        return 1;
    }catch(Exception e){
        return 0;
    }
}

我使用了两种格式作为参数通过axios传递,

1.一种是直接传递v-model中的值,但是在调试的时候notwork中我传输的数据是form data,我也不清楚什么原因,只能使用fromJson工具转换成json格式,然后再后端再转换成String类型

2.使用axios推荐的格式

            var args = {
                str:this.q.orgs.value
            };

在后端直接用@RrequestBody String str进行接收即可

发布了89 篇原创文章 · 获赞 42 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/cjhxydream/article/details/100703668
今日推荐