相关疑难解决方法(0)

Spring JSON请求获得406(不可接受)

这是我的javascript:

    function getWeather() {
        $.getJSON('getTemperature/' + $('.data option:selected').val(), null, function(data) {
            alert('Success');                               
        });
    }
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

@RequestMapping(value="/getTemperature/{id}", headers="Accept=*/*", method = RequestMethod.GET)
@ResponseBody
public Weather getTemparature(@PathVariable("id") Integer id){
    Weather weather = weatherService.getCurrentWeather(id);
        return weather;
}
Run Code Online (Sandbox Code Playgroud)

为spring-servlet.xml

<context:annotation-config />
<tx:annotation-driven />
Run Code Online (Sandbox Code Playgroud)

得到此错误:

GET http://localhost:8080/web/getTemperature/2 406 (Not Acceptable)
Run Code Online (Sandbox Code Playgroud)

头:

响应标题

Server  Apache-Coyote/1.1
Content-Type    text/html;charset=utf-8
Content-Length  1070
Date    Sun, 18 Sep 2011 17:00:35 GMT
Run Code Online (Sandbox Code Playgroud)

请求标题

Host    localhost:8080
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept  application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, …
Run Code Online (Sandbox Code Playgroud)

javascript java ajax spring json

83
推荐指数
7
解决办法
18万
查看次数

HttpMediaTypeNotAcceptableException

我的jQuery函数有问题我想要实现的是在列表框中填充数据

JavaScript函数

function load() {
        $.getJSON('${findAdminGroupsURL}', {
            ajax : 'true'
        }, function(data) {
            var html = '<option value="">Groups</option>';
            var len = data.length;
            for ( var i = 0; i < len; i++) {
                html += '<option value="' + data[i].name + '">' + data[i].name
                        + '</option>';
            }
            html += '</option>';

            $('#selection').html(html);
        });
    }
Run Code Online (Sandbox Code Playgroud)

服务器端是

@RequestMapping(value = "groups", method = RequestMethod.GET)
    public @ResponseBody
    List<Group> getGroups() {
        return this.businessGroups();
    }
Run Code Online (Sandbox Code Playgroud)

我在load上调用load()函数它会触发函数getGroups()并成功返回列表,但问题是getGroups()完成后

函数(数据)不加载永远不会进入该函数,错误是

org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示

我不能回发组对象列表,还是必须是Java原始类型?

javascript java jquery spring json

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

标签 统计

java ×2

javascript ×2

json ×2

spring ×2

ajax ×1

jquery ×1