我试图让城市的名单通过阿贾克斯用SpringMVC我3.0项目发送中华人民共和国的国家名称。 为此目的,我用下面的调用(使用jQuery)在我的JSP:
I'm trying to get a list of Cities by sending the State name through Ajax in my SpringMVC 3.0 project. For the purpose, I've used the following call (using jQuery) in my JSP:
<script type="text/javascript">
function getCities() {
jq(function() {
jq.post("getCities.html",
{ stateSelect: jq("#stateSelect").val()},
function(data){
jq("#cities").replaceWith('<span id="cities">Testing</span>');
});
});
}
</script>
这是我的控制器code:
And here's my Controller code:
@RequestMapping(value = "/getCities", method = RequestMethod.POST)
public @ResponseBody List<StateNames> getCities(@RequestParam(value="stateSelect", required=true) String stateName,
Model model) {
// Delegate to service to do the actual adding
List<StateNames> listStates = myService.listCityNames(stateName);
// @ResponseBody will automatically convert the returned value into JSON format
// You must have Jackson in your classpath
return listStates;
}
不过,我得到HTTP 406错误,说明如下当我运行它: 406不可接受 所请求的资源只能根据请求发送的接受头部产生的内容不能接受的。
But I get HTTP 406 error stating the following when i run it: 406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.
我用杰克逊在我的Maven的依赖关系和放大器;在我的背景文件中所定义。 我已经广泛地与放大器一派;我想这个问题是@ResponseBody不会自动转换我的列表,以适当的JSON对象。
I've used Jackson in my Maven dependencies & have defined in my context file. I've googled extensively & I guess the problem is @ResponseBody is not automatically converting my List to appropriate JSON object.
我的萤火虫说:
Response Headers
Server Apache-Coyote/1.1
Content-Type text/html;charset=utf-8
Content-Length 1070
Date Sat, 12 Feb 2011 13:09:44 GMT
Request Headers
Host localhost:8080
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://localhost:8080/MyApplication/
Content-Length 17
Cookie JSESSIONID=640868A479C40792F8AB3DE118AF12E0
Pragma no-cache
Cache-Control no-cache
请指引我。我究竟做错了什么?帮助!
Please guide me. What am i doing wrong?? HELP!!
正如彼得写在他的意见,问题的原因是春无法加载杰克逊。它不会被依赖默认加载。之后我已经添加了依赖
As Peter had written in his comment, the cause of the problem is inability of Spring to load Jackson. It is not loaded by dependencies by default. After I've added the dependency
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.2</version>
</dependency>
JSON的打字在浏览器的地址后返回,而不会接受头(因为它是应该做)什么花样。
the JSON was returned after typing the address in the browser, without any tricks with Accept headers (as it is supposed to do).
测试在Tomcat 7.0。
Tested on Tomcat 7.0.