求解: jquery 发送的 put/delete 请求里的参数在 springmvc 里怎么接收?

2019-02-25 14:25:20 +08:00
 tomoya92

jquery 请求:

$.ajax({
  url: '/api/topic/${topic.id}',
  type: 'put',
  cache: false,
  async: false,
  dataType: 'json',
  headers: {
    "token": "${_user.token}",
  },
  data: {
    title: title,
    content: content,
    tags: tags,
  },
  success: function (data) {
    if (data.code === 200) {
      window.location.href = "/topic/" + data.detail.id
    } else {
      alert(data.description);
    }
  }
})

后台 controller:

@PutMapping(value = "/api/topic/{id}")
public Result edit(@PathVariable Integer id, String title, String content, String tags) {
  System.out.println(title);
  System.out.println(content);
  System.out.println(tags);
  return success();
}

打印出来的结果全是 null,id 是有的

chrome 审查元素看了下,jquery 把 put 和 delete 请求都封装成 form data 后才发送的请求,但就算是 form data 格式的参数在后台也可以接收到的吧!!

求 v2 大佬解惑,万分感谢

1556 次点击
所在节点    问与答
4 条回复
night98
2019-02-25 14:46:50 +08:00
springmvc 对于 put 和 delete 请求不会接收参数。可以考虑使用 url param
night98
2019-02-25 14:47:18 +08:00
准确来说是不解析 body 内容
tomoya92
2019-02-25 14:49:32 +08:00
@night98 #1 用 /api/topic/1?title=xx&content=xx&tags=xx 这种形式吗?
tomoya92
2019-02-25 15:30:29 +08:00
@night98 解决了,在程序里任意一个地方加上这个 bean 就可以了

```java
@Bean
public FormContentFilter formContentFilter() {
return new FormContentFilter();
}
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/538472

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX