(1)axios.defaults.headers.post['Content-Type'] = 'multipart/form-data';
传参格式为 formData==>

var formData=new FormData();
formData.append('user',123456);
formData.append('pass',12345678);
 
axios.post("/notice",formData)
     .then((res) => {return res})
     .catch((err) => {return err})

(2)axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
传参格式为 query 形式,使用$qs.stringify

import axios from 'axios'
import qs from 'Qs'
let data = {"code":"1234","name":"yyyy"};
axios.post(`${this.$url}/test/testRequest`,qs.stringify({
    data
}))
.then(res=>{
    console.log('res=>',res);            
})

(3) axios.defaults.headers.post['Content-Type']= 'application/json;charset=utf-8'
传参格式为 raw (JSON格式)(常用)

let user= {
                        id:'123',
                        name:'七七七',
                        sex:'男',
                        age:19,
                        school:'攀大'
                    };
                axios({
                        url:'http://localhost:8081/addUser',
                        method:'post',
                        data:JSON.stringify(user),
                    }).then(
                    (response)=>
                    {
                        console.log(response.data)
                    },
                    (error)=>
                    {
                        console.log(error.message)
                    }

后台(接收的是一个对象)为:

@GetMapping
    @RequestMapping(path = "/addUser")
    public String addUser(@RequestBody Test t)
    {
        System.out.println(t.getId());
        return "增加成功";
    }

Logo

前往低代码交流专区

更多推荐