0%

springBoot上传一直为空

文章字数:158,阅读全文大约需要1分钟

ajax提交json数据,springBoot使用有相同类型的实体类接收,但是实体类属性全部是空。最后通过前端F12查看发送的数据,发现发送的是json字符串,而不是键值对形式。原来是我在前端多了一步转换对象为json字符串的操作。。。低级错误。。

正确的代码结构:
前端提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var formdata={};
formdata.name='xxx';
formdata.pwd='zzz';
$.ajax({
data: formdata,
dataType: "json",
type: "post",
url: "/log",
success: function(result) {
console.log(result)
},
error: function(result) {

}
});

springBoot:

1
2
public Mes add(User u,@RequestParam("other") String other){//除了实体类里的信息外指定name获取其他信息
}

其中,User实体类应该完全包含前端发送的json(formdata)的所有属性,否则可能会出错。