V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
tomoya92
V2EX  ›  问与答

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

  •  
  •   tomoya92 · 2019-02-25 14:25:20 +08:00 · 1552 次点击
    这是一个创建于 1904 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 大佬解惑,万分感谢

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

    ```java
    @Bean
    public FormContentFilter formContentFilter() {
    return new FormContentFilter();
    }
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1132 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:54 · PVG 06:54 · LAX 15:54 · JFK 18:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.