2017-06-10 7 views
2

gamesetting.jsp上にあるfullCalendarにJSON配列を送信しようとしています。 json配列オブジェクトはコンソール経由でテストされていて、 これは正解を示しています。私は$( "#calendar")を試しました。fullCalendar( 'addEventSource'、source)、クロムは406エラーを返し、応答はThe resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.スプリング@ResponseBodyのFullcalendarがJson Arrayを406エラーで返します:受け入れられないヘッダー

私は本当に間違って何見当もつかないコンソール

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

であるに示す

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

...

+0

この行を削除するとどうなるでしょうか?response.setHeader(" Accept "、" application/json ");' –

+0

@Sagar Rohankar - レスポンスヘッダーには受け入れられないプロパティがあり、同じ記述で406エラーが返されます。 –

答えて

1

あなたはResponseEntityにJSONObjectからResponseBodyを変更することでこの問題を解決することができます
だから、サーバー側のコードは次のとおりです。

この上
@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が発生しましたサーバーが要求で指定されたaccept-headerで応答できない場合あなたのケースでは、アプリケーションの応答/ jsonがサーバに受け入れられないかもしれません。 "

+1

wow!はあなたの言ったとおりに動作します!406がなくなり、このResponseBodyにはDB Dataが含まれています。 –

関連する問題