问题描述:
在使用@PutMapping接收String类型参数时发现接收到的数据多两个引号
1.前端代码
updateStep7ImageName(orgId, projectId, id, name) {
return ajax.put(`orgs/${orgId}/project/report/${projectId}/step7/img/${id}/name`, name)
},
2.后端代码
@PutMapping("/{projectId}/step7/img/{id}/name")
@Permission("project:edit")
public Result updateStep7ImageName(@RequestBody String name,@PathVariable("projectId") String projectId, @PathVariable("id") Integer id) {
return Result.success("修改成功", step7ImagesService.updateStep7ImageName( projectId,WebUtil.getUserOrgId(),id, name));
}
后端Debug接收到的数据,发现是JSON格式的,保存到数据库后多出来两个双引号
3. 解决
前端代码请求头加上: ‘Content-Type’: ‘text/plain’
updateStep7ImageName(orgId, projectId, id, name) {
return ajax.put(`orgs/${orgId}/project/report/${projectId}/step7/img/${id}/name`, name, {
headers: { 'Content-Type': 'text/plain' }
})
},
原因
请求默认是JSON格式的数据Content-Type:application/json
修改前请求头:
修改后请求头
##参考
https://github.com/axios/axios/issues/4034
https://blog.csdn.net/weixin_43852792/article/details/124142601