Reputation: 11
I am using Ajax with Spring MVC and cannot get the success function to execute.
Here is my 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;
}
}
This works perfectly. I can add a break point and step through the code in debug with no problem.
Here is the ajax call and related html in code from my JSP page:
<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>
When I add a text to the input the Spring controller is executed but nothing happens on the browser side.
There are no exceptions thrown in the and the console says, "Successfully completed request"
This is my first Ajax call so I'm pretty green at this.
Thank you in advance.
Upvotes: 0
Views: 562
Reputation: 11
I figured this out. I did not realize you could get the errors by using the error option in the .ajax function so I converted the getJson to a ajax function and examined the error. The problem was I did not have the necessary jars to convert to json. Here are the Maven dependencies I added:
<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>
Upvotes: 1