第一种:
var user= {
"username" : username,
"password" : password,
"rememberMe":rememberMe
};
$.ajax({
url : "http://...../jsontest.do",
type : "POST",
async : true,
data : user,
dataType : 'json',
success : function(data) {
}
});
此时请求的ContentType默认是application/x-www-form-urlencoded这种方式,后端可以用对象或者字符串直接接收
第二种:如果后端加上@RequestBody注解,则前端必须是post请求的json格式,因为RequestBody注解接收的是请求体,而且需要使用JSON.stringify()将JSON对象转化为JSON字符串,还不能用字符串接收
var user= {
"username" : username,
"password" : password
};
$.ajax({
url : "http://...../jsontest.do",
type : "POST",
async : true,
contentType: "application/json; charset=utf-8",
data : JSON.stringify(user),
dataType : 'json',
success : function(data) {
}
});
网友评论