【问题标题】:Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'search' is not present]已解决 [org.springframework.web.bind.MissingServletRequestParameterException:必需的字符串参数“搜索”不存在]
【发布时间】:2020-10-16 20:31:33
【问题描述】:

我的控制器无法选择参数。 seach 参数保持为空

@GetMapping("/api/search")
public ResponseEntity<List<ProductDto>> search(@RequestParam("search") String search) {  
    return new ResponseEntity<>(searchProductDao.search(search), HttpStatus.OK);
}

这里是html部分:

<form class="form-header" onsubmit="return false;" method="GET">
    <input class="au-input au-input-xl" id="search" type="text" name="search"
                                placeholder="Search for data ..." />
        <button class="au-btn-submit" type="submit">
                                Search
        </button>
</form>

和Ajax方法:

$('#search').on('keyup', function () {
    // $('.product-box').append(output);
    let text = $('#search').val();
    if(text != '') {
        $.ajax({
            url: '/api/search',
            type: 'GET',
            dataType: 'json',
            data: text,
            contentType: text,
            success: function (listOfDtoProducts) {
                $.each(listOfBooks, function (i, p) {
                    $('.product-box').append(fillingBlockOfProduct(p));
                })
            }
        })
    }
})

})

函数显示Books() { $.ajax({ url: '/api/books', 类型:'GET', 数据类型:'json', 成功:函数(listOfBooks){ $.each(listOfBooks, function (i, book) { $('.product-box').append(fillingBlockOfProduct(book)); }); },

【问题讨论】:

  • 请提供你的控制器的错误和路径映射

标签: html ajax spring-boot


【解决方案1】:

看起来您只是将搜索输入值发送到 ajax 查询。

弹簧控制器正在寻找一个名为“搜索”的参数

而不是

data: text,

试试

data:'search='+text,

let dataObj = {'search': text};
...
data: dataObj,

【讨论】:

  • 是的,但我认为正确的解决方案是从 html 获取 RequestParam。请看javatpoint.com/…
  • 我不明白。在这里,您使用 jquery ajax 发送日期而不是浏览器表单提交。
猜你喜欢
  • 2021-03-28
  • 2016-09-03
  • 2015-11-20
  • 2017-10-26
  • 2019-09-23
  • 2021-04-01
  • 2018-05-03
  • 1970-01-01
  • 2017-11-27
相关资源
最近更新 更多