我正在使用 Spring 并尝试在我的控制器中对 @ResponseBody 进行 ajax 调用。
更新
好的,我将我被告知的更改添加到我的 ajax 设置中。我的参数“jtSearchParam”在 IE 中仍然存在相同的编码问题。+ 我收到另一个错误,406,响应标头的内容类型错误。
这是我的新代码
控制器:
@RequestMapping(method = RequestMethod.POST, consumes="application/json; charset=utf-8", produces="application/json; charset=utf-8")
public @ResponseBody JSONObject getUsers(@RequestParam int jtStartIndex, @RequestParam int jtPageSize,
@RequestParam String jtSorting, @RequestParam String jtSearchParam,
HttpServletRequest request, HttpServletResponse response) throws JSONException{
Gson gson = new GsonBuilder()
.setExclusionStrategies(new UserExclusionStrategy())
.create();
List<User> users = userService.findUsers(jtStartIndex ,jtPageSize, jtSorting, jtSearchParam);
Type userListType = new TypeToken<List<User>>() {}.getType();
String usersJsonString = gson.toJson(users, userListType);
int totalRecordCount = userDao.getAmountOfRows(jtSearchParam);
usersJsonString = "{\"Message\":null,\"Result\":\"OK\",\"Records\":" + usersJsonString + ",\"TotalRecordCount\":" + totalRecordCount + "}";
JSONObject usersJsonObject = new JSONObject(usersJsonString);
return usersJsonObject;
}
如您所见,我设置了内容类型,produces
但这无济于事。如果我调试响应标头,它看起来像这样:(这会导致浏览器出现 406 Not Acceptable)
还有我的新 ajax 设置:
...
headers: {
Accept : "application/json; charset=utf-8",
"Content-Type": "application/json; charset=utf-8"
},
contentType: "application/json; charset=utf-8",
mimeType:"application/json; charset=UTF-8",
cache:false,
type: 'POST',
dataType: 'json'
...
而且我的参数在 IE 中看起来还是一样的!