我正在尝试将jQuery与Spring MVC结合使用,以从数据库获取“ bookid”,但每次都会收到错误提示

时间:2019-05-20 13:37:23

标签: java jquery spring-mvc jsp

嗨,我在Spring MVC中使用jQuery从数据库中获取book_id,但每次都收到错误提示

我已经使用jQuery ajax()方法并正确定义了所有参数,例如url,并且在Controller中我已经使用了所有正确的注释,但是未能获得输出。

$(document).ready(function () {
    $("#bookId").keypress(function () {
                    var bookid = $(this).val();
                    alert($(this).val());
                    $.ajax({
                    url: "getBookQuantity.htm",
                            data: {
                            bookid : bookid
                            },
                            dataType: "json",
                            success: function (json) {
                                if (json !== null) {
                                    alert(json);
                                }
                            },       
                            error: function (e) {
                                alert("Some error occured while getting book id list");
                            }      
                });
            });
        });

控制器代码:

@RequestMapping(value = "/getBookQuantity.htm")
 @ResponseBody
 public String getBookQuantity(@RequestParam(value = "bookid", defaultValue = "0") int bookid) {
    System.out.println("bookid======="+bookid);
    int quantity = this.bookService.checkBookQuantity(bookid);

    String json = null;
    Gson gson = new Gson();
    json = gson.toJson(quantity);
    return json;
}

每次都会收到错误警报

0 个答案:

没有答案