关于Jquery与Axios发送ajax请求时的请求体问题

jquery
jquery发送post请求时,会把data中的请求参数以查询字符串的方式封装在请求体中,如

data:{
    
    name:"zs",age:18}

则此时请求体中的数据为name=zs&age=18,后端通过@RequestBody String json 可以获取请求体中的数据,但是是查询字符串方式的,不是json格式的,此时也可以通过
request.getParmeter(“name”)的方式获取。

如何用jquery封装JSON格式的请求体
后端如果想直接将请求体中的数据转为业务模型,如 @RequestBody User user
,则是不行的,因为此时的请求体类型为application/x-www-form-urlencoded ,而要想封装为业务模型,请求体内容类型必须为application/json;charset=utf-8

public class User{
    
    
	private String name;
	private Integer age;
}

所以此时需要将jquery的contentType类型设置为:application/json;charset=utf-8
同时将data参数的对象转为json字符串

contentType:"application/json;chatset=utf-8",
data:JSON.stringify({
    
    name:"zs",age:18})

猜你喜欢

转载自blog.csdn.net/qq_43750656/article/details/119252413