美文网首页
[SpringMVC] AJAX请求提交数据无法进入Contro

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

作者: 你说这个谁懂啊 | 来源:发表于2020-03-13 21:29 被阅读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)

相关文章

网友评论

      本文标题:[SpringMVC] AJAX请求提交数据无法进入Contro

      本文链接:https://www.haomeiwen.com/subject/wwsyshtx.html