小编Ton*_*ami的帖子

Spring 3 + JSON的问题:HTTP状态406?

我试图通过在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>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器代码:

@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;
    }
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我收到HTTP 406错误说明以下内容: 406 Not Acceptable请求的资源只能根据请求中发送的Accept标头生成不可接受的内容.

我在我的Maven依赖项中使用了Jackson,并在我的上下文文件中定义了.我已经广泛搜索了,我猜问题是@ResponseBody不会自动将我的List转换为适当的JSON对象.

我的Firebug说:

Response …
Run Code Online (Sandbox Code Playgroud)

ajax jquery json spring-mvc

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

ajax ×1

jquery ×1

json ×1

spring-mvc ×1