axios post请求 后台使用@requestBody 无法接收数据


//前台
 saveHero(){
                axios({
                    method:this.requestType,//post
                    url:'hero',
                    data:this.hero
                }).then(res=>{
                    if(res.data.code="200"){
                        debugger
                        alert("保存成功");
                        //关闭模态
                        $("#myModal").modal('hide')
                        // 刷新页面
                        this.listHero()
                    }else{
                        alert("保存失败");
                    }
                })
            },
@PostMapping
    public Map<String, Integer> add(@RequestBody  Hero hero) {
         Hero hero1 = this.heroService.insert(hero);
         Map<String,Integer> map = new HashMap<>();
         if(hero1 != null){
            map.put("code", 200);
         }else{
            map.put("code", 401);
         }
         return map;
    }

img

把this.hero JSON.stringify 一下

尝试下这种写法,明确指定Content-Typeapplication/json;charset=UTF-8

axios.post(url, JSON.stringify(params),{
            headers: {
                'Content-Type': 'application/json;charset=UTF-8'
            }
         })
        .then(res => {
            resolve(res.data);
        })
        .catch(err =>{
            reject(err.data)
        })

各位 是我疏忽了 是类型不匹配导致的 谢谢大家提供的思路