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

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)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容