2

I am trying to send a JSON Array to my fullCalendar which locate on gamesetting.jsp. the json array object has been test through console and It has showing the cerrect answer.then I tried $("#calendar").fullCalendar( 'addEventSource', source ), chrome give back a 406 error and want the response asThe 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;
}

shown in console

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

and this is my JavaScript code

    $(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);    
                    }                         
             }
        },

I really have no clue what's wrong...

Bill EE
  • 31
  • 5
  • What happens when you remove this line `response.setHeader("Accept", "application/json");` – sagarr Jun 10 '17 at 09:06
  • @Sagar Rohankar-the response header would have no accept property and still return 406 error with the same description. – Bill EE Jun 10 '17 at 13:51

1 Answers1

1

You can solve this by changing the ResponseBody from JSONObject to ResponseEntity:
So the server side code is:

@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);
}

More on this:
From this page: What is "406-Not Acceptable Response" in HTTP? : "406 happens when the server cannot respond with the accept-header specified in the request. In your case it seems application/json for the response may not be acceptable to the server."

Jack
  • 86
  • 1
  • 6
  • 1
    wow! works just as you said!! 406 has gone and this ResponseBody contain the DB Data.Sincerely thanks for your sharing ! – Bill EE Jun 10 '17 at 14:01