具有Ajax成功功能的Spring-MVC无法执行

时间:2016-08-26 16:40:37

标签: json ajax jsp spring-mvc

我在Spring MVC中使用Ajax并且无法获得执行的成功函数。

这是我的Spring Controller

@RequestMapping("looseSearch")
    public @ResponseBody List<Book> search(@RequestParam("CHARS") String chars) {
        List<Book> books = new ArrayList<Book>();
        books.add(new Book("Star Treck", "Bettle Bum"));
        return books; 
    }
}

这完美无缺。我可以添加一个断点并在调试中逐步执行代码。

这是来自我的JSP页面的代码中的ajax调用和相关的html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
    function doSearch() {
        $.getJSON("looseSearch", {
            CHARS : $('#searchBox').val()
        },

        function(data) {
            alert("Response Received" + data);
        });
    }
</script>
</head>
<body>
    <input id="searchBox" type="text" onKeyUp="doSearch();" />
    <div id="results>Results will appear here....</div>
</body>
</html>

当我在输入中添加文本时,Spring控制器被执行但浏览器端没有任何操作。

没有抛出异常,控制台说“成功完成请求”

这是我的第一个Ajax调用,所以我对此非常了解。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

我想出来了。我没有意识到你可以通过使用.ajax函数中的error选项来获取错误,所以我将getJson转换为ajax函数并检查错误。问题是我没有必要的罐子转换为json。以下是我添加的Maven依赖项:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.5.1</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.5.1</version> 
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.5.1</version>
</dependency>
相关问题