【发布时间】:2011-06-26 08:38:54
【问题描述】:
我试图在我的 SpringMVC 3.0 项目中通过 Ajax 发送州名称来获取城市列表。 为此,我在 JSP 中使用了以下调用(使用 jQuery):
<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>
这是我的控制器代码:
@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 不可接受 请求的资源只能生成根据请求中发送的 Accept headers 不可接受的内容。
我在我的 Maven 依赖项中使用了 Jackson,并在我的上下文文件中进行了定义。 我已经广泛搜索,我猜问题是@ResponseBody 没有自动将我的列表转换为适当的 JSON 对象。
我的萤火虫说:
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
请指导我。我究竟做错了什么??救命!!
【问题讨论】:
标签: ajax json jquery spring-mvc