带有spring @ResponseBody的Fullcalendar返回带有406错误的Json数组:不可接受的标题

时间:2017-06-10 09:00:41

标签: javascript arrays json spring fullcalendar

我正在尝试将一个JSON数组发送到我的fullCalendar,它位于gamesetting.jsp上。 json数组对象已通过控制台和测试 它显示了正确的答案。然后我尝试了$(“#calendar”)。fullCalendar('addEventSource',source),chrome回复了406错误,希望响应为+----+---------------+ | ID | NEW_NAME | +----+---------------+ | 1 | Ram Kumar | | 2 | Rajesh A | | 3 | Avinash K Bae | | 4 | Kishore Babu | +----+---------------+

<ul class="nav nav-tabs tabIcon" role="tablist">
    <li class="condos">
        <a href="#condos" role="tab" data-toggle="tab">
            <i class="fa fa-university" aria-hidden="true"></i>
            <span class="tablink">Condos</span>
        </a>
    </li>
    <li class="cruises">
        <a href="#cruises" role="tab" data-toggle="tab">
            <i class="fa fa-ship" aria-hidden="true"></i>
            <span class="tablink">Cruises</span>
        </a>
    </li>
    <li class="packages">
        <a href="#packages" role="tab" data-toggle="tab">
            <i class="fa fa-suitcase" aria-hidden="true"></i>
            <span class="tablink">Packages</span>
        </a>
    </li>
    <li class="flights">
        <a href="#flights" role="tab" data-toggle="tab">
            <i class="fa fa-plane" aria-hidden="true"></i>
            <span class="tablink">Flights</span>
        </a>
    </li>
    <li class="cars">
        <a href="#cars" role="tab" data-toggle="tab">
            <i class="fa fa-car" aria-hidden="true"></i>
            <span class="tablink">Cars</span>
        </a>
    </li>
    <li class="hotel">
        <a href="#hotel" role="tab" data-toggle="tab">
            <i class="fa fa-building-o" aria-hidden="true"></i>
            <span class="tablink">Hotels</span>
        </a>
    </li>
</ul>





<div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="hotel">
       <content 1>
    </div>
    <div role="tabpanel" class="tab-pane" id="cars">
        <content 2>
    </div>
    <div role="tabpanel" class="tab-pane" id="flights">
        <content 3>
    </div>
    <div role="tabpanel" class="tab-pane" id="condos">
       <content 4>
    </div>
    <div role="tabpanel" class="tab-pane" id="packages">
        <content 5>
    </div>
    <div role="tabpanel" class="tab-pane" id="cruises">
        <content 6>
    </div>
</div>





<script>
$(document).ready(function() {
    $(".hotel").click(function () {
        $(".slice1").addClass("imgActive");
    });
    $(".cars").click(function () {
        $(".slice2").addClass("imgActive");
    });
    $(".flights").click(function () {
        $(".slice3").addClass("imgActive");
    });
    $(".packages").click(function () {
        $(".slice4").addClass("imgActive");
    });
    $(".cruises").click(function () {
        $("slice5").addClass("imgActive");
    });
    $(".condos").click(function () {
        $(".slice6").addClass("imgActive");
    });
});
</script>



<!-- images-->
<ul>
    <li>
        <img src="~/Content/Images/rccship.png" class="imgSlide slice1 imgActive"/>
    </li>
    <li>
        <img src="~/Content/Images/overwaterbungalowsresized-3.png" class="imgSlide slice2"/>
    </li>
    <li>
        <img src="~/Content/Images/hiker.png" class="imgSlide slice3"/>
    </li>
    <li>
        <img src="~/Content/Images/santorini1.png" class="imgSlide slice4"/>
    </li>
    <li>
        <img src="~/Content/Images/airplanerevised.png" class="imgSlide slice5"/>
    </li>
    <li>
        <img src="~/Content/Images/beachcondos.png"class="imgSlide slice6"/>
    </li>
</ul>
<!--/images-->

显示在控制台

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

这是我的JavaScript代码

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

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

1 个答案:

答案 0 :(得分: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?:&#34; 406在服务器无法使用请求中指定的accept-header进行响应时发生。在您的情况下,似乎应用程序的application / json可能不被服务器接受。&#34;