axios @RequestBody @RequestParam

2022-10-30 11:55:48

总结:

前端 headers中的 Content-Type 使用什么类型,axios会根据 post请求中的参数是否使用 qs 序列化来自动生成
当后端使用 @RequestBody,前端 不能使用qs序列化参数, Content-Type 默认是   'Content-Type':  application/json;charset=UTF-8,
当后端使用 @RequestParam,前端需要使用 qs序列化参数, Content-Type  默认是    'Content-Type': 'application/x-www-form-urlencoded',

// 如果后端使用 @RequestBody接收,前端transformRequest就不能使用 qs
// return http.post('/api/v1/pri/user/loginTest',dataParams,{            // @RequestBody     application/json;charset=UTF-8
return http.post('/api/v1/pri/user/loginTest',qs.stringify(dataParams),{ //  @RequestParam   application/x-www-form-urlencoded
        headers:{
        // 'Content-Type': 'application/json;charset=UTF-8',
        // 'Content-Type': 'application/x-www-form-urlencoded',
    }
})
    .then((res) => {
        return data;
    })
}
  • 作者:海马区
  • 原文链接:https://blog.csdn.net/qq_40820382/article/details/122038336
    更新时间:2022-10-30 11:55:48