我正在尝试将 JSON 数组发送到位于 gamesetting.jsp 上的 fullCalendar。 json 数组对象已通过控制台进行测试,并且
它显示了正确的答案。然后我尝试了 $("#calendar").fullCalendar( 'addEventSource', source ),chrome 返回 406 错误并希望响应为 The resource identified by this request is only capable根据请求“接受” header 生成具有 Not Acceptable 特征的响应。
@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);
}
}
},
我真的不知道哪里出了问题......
最佳答案
您可以通过将 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? : "当服务器无法响应请求中指定的 accept-header 时,会发生 406。在您的情况下,服务器可能无法接受响应的 application/json。"
关于javascript - Fullcalendar with spring @ResponseBody 返回带有 406 错误 :not Acceptable Header 的 Json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44471486/