[SpringMVC] AJAX请求提交数据无法进入Contro

2020-03-13  本文已影响0人  你说这个谁懂啊

AJAX请求参数为JSON格式时,Controller方法只能有一个对象参数,并要添加@RequestBody注解

axios.post('/url', { field1: "a", field2: "b" })
  .then(function (response) { })
  .catch(function (error) { });
public ResponseEntity<Void> save(@RequestBody Entity entity)

下面是错误的方法参数

public ResponseEntity<Void> save(Entity entity)
public ResponseEntity<Void> save(Entity entity, String ids)

将JSON格式的请求参数转为 application/x-www-form-urlencoded 格式可绕过上面的限制

let params = {
  entity: entity,
  ids: "1,2,3",
  flag: "a"
};
axios.post('/url', qs.stringify(params))
  .then(function (response) { })
  .catch(function (error) { });
public ResponseEntity<Void> save(Entity entity, List<String>ids, String flag)
上一篇 下一篇

猜你喜欢

热点阅读