Reputation: 47146
I get tried implement ajax in my spring3 mvc application. Ajax worked fine when I was returning a simple string but when I tried returning a list of strings I got a 406 error
.
This is my context
file
<context:annotation-config />
<context:component-scan base-package="com.ajaxtest" />
<mvc:annotation-driven />
<import resource="hibernate-context.xml" />
<mvc:resources mapping="/resources/**" location="/resources/"/>
This is my controller mapping method
@RequestMapping(value="/test", method = RequestMethod.GET,
headers="Accept=*/*")
public @ResponseBody List<String> testAjax(@RequestParam("query") String query) {
System.out.println(query);
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
list.add(""+i);
}
return list;
}
This is my js code.
$(function(){
$.get('ajax/test.html',{'query' : 1},function(data){
console.log(data);
});
});
I have added the following jar files for mapping.
jackson-core-2.0.2.jar, jackson-core-asl-1.9.7.jar, jackson-datatype-json-org-2.0.2.jar, jackson-mapper-asl-1.9.7.jar
What changes should I do to return a list of objects back to js?
Upvotes: 2
Views: 305
Reputation: 299218
HTTP Error Code 406 stands for Bad Client Request (see HTTP error codes)
The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.
You need to set the Accept Header to application/json
. In JQuery, you do that by using jQuery.getJSON()
Upvotes: 2