【问题标题】:Fullcalendar with spring @ResponseBody return Json Array with 406 error :not Acceptable Header带有弹簧 @ResponseBody 的 Fullcalendar 返回带有 406 错误的 Json 数组:不可接受的标题
【发布时间】:2017-11-12 06:43:06
【问题描述】:

我正在尝试将 JSON 数组发送到位于 gamesetting.jsp 上的 fullCalendar。 json 数组对象已通过控制台进行了测试,并且 它显示了正确的答案。然后我尝试了 $("#calendar").fullCalendar( 'addEventSource', source ),chrome 返回 406 错误并希望响应为The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

@RequestMapping ( method=RequestMethod.GET ,produces={"application/json; charset=UTF-8"}) 
public @ResponseBody JSONObject returnGames(GameVO gameVo,HttpServletResponse response) throws IOException{
    GameService gService= new GameService(gameDao); 
    List<GameVO> games= gService.select(gameVo);
    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObj =null;
    for(GameVO game:games){
        jsonObj = new JSONObject();
        jsonObj.put("game_no", game.getGame_sd());
        jsonObj.put("game_name", game.getGame_name());
        jsonObj.put("game_time", game.getGame_time());
        jsonArray.put(jsonObj);
    }
    System.out.println("jsonArray: "+jsonArray);
    response.setHeader("Accept", "application/json");
 return jsonObj;
}

显示在控制台中

jsonArray: [{"game_name":"A vs B","game_time":"2015-05-20 00:00:00.0","game_no":1}]

这是我的 JavaScript 代码

    $(document).ready(function() {
    $('#calendar').fullCalendar({
            customButtons: {
                myCustomButton: {
                    text: 'custom!',
                    click: function getGames(e){

                        var games;
                        var url='selectAllGames.controller';
                        if (getGames){
                            $.getJSON(url,null,
                                 function(){$("#calendar").fullCalendar( 'addEventSource',url )})
                                 ;}                   
                    console.log(e);    
                    }                         
             }
        },

我真的不知道出了什么问题......

【问题讨论】:

  • 删除此行时会发生什么response.setHeader("Accept", "application/json");
  • @Sagar Rohankar - 响应标头将没有接受属性,并且仍然返回具有相同描述的 406 错误。

标签: javascript arrays json spring fullcalendar


【解决方案1】:

您可以通过将 ResponseBody 从 JSONObject 更改为 ResponseEntity 来解决此问题:
所以服务端代码是:

@RequestMapping(method=RequestMethod.GET, produces={"application/json; charset=UTF-8"}) 
public @ResponseBody ResponseEntity<String> returnGames(GameVO gameVo, HttpServletResponse response) throws IOException {
    GameService gService = new GameService(gameDao); 
    List<GameVO> games = gService.select(gameVo);
    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObj = null;
    for(GameVO game : games){
        jsonObj = new JSONObject();
        jsonObj.put("game_no", game.getGame_sd());
        jsonObj.put("game_name", game.getGame_name());
        jsonObj.put("game_time", game.getGame_time());
        jsonArray.put(jsonObj);
    }
    //System.out.println("jsonArray: "+jsonArray);
    //response.setHeader("Accept", "application/json");

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "application/json; charset=utf-8");

    return new ResponseEntity<String>(jsonArray.toString(), responseHeaders, HttpStatus.OK);
}

更多信息:
从此页面:What is "406-Not Acceptable Response" in HTTP?:“当服务器无法使用请求中指定的接受标头进行响应时,会发生 406。在您的情况下,服务器似乎无法接受响应的 application/json。”

【讨论】:

  • 哇!就像你说的那样工作!! 406 已消失,此 ResponseBody 包含 DB 数据。衷心感谢您的分享!
猜你喜欢
  • 2012-06-17
  • 2011-11-12
  • 2014-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 2012-01-29
相关资源
最近更新 更多