Jam*_*ter 5 ajax jquery encoding spring spring-mvc
我正在使用Spring并尝试在我的控制器中对@ResponseBody进行ajax调用.
UPDATE
好的,我把我告诉的更改添加到了我的ajax设置中.我的参数"jtSearchParam"在IE中仍然存在相同的编码问题.+我收到另一个错误,406,响应Header的内容类型错误.
这是我的新代码
控制器:
@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;
}
Run Code Online (Sandbox Code Playgroud)
所以当你看到我设置了内容类型,produces但这没有帮助.如果我调试响应头它看起来像这样:(这导致406不能从浏览器接受)

我的新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'
...
Run Code Online (Sandbox Code Playgroud)
我的参数在IE中看起来仍然一样!

好的,json内容类型的问题可以像这样解决:
使用ResponseEntity,您可以更改响应头的内容类型,这样ajax可以正确解释json对象,并且您不会得到406 Http错误.
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> getUsers(@RequestParam int jtStartIndex, @RequestParam int jtPageSize,
@RequestParam String jtSorting, @RequestParam String jtSearchParam,
HttpServletRequest request, HttpServletResponse response) throws JSONException{
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "application/json; charset=utf-8");
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 + "}";
return new ResponseEntity<String>(usersJsonString, responseHeaders, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
编码问题可以像这样解决:
IE不会编码你的"ü,ä等" 正确地说,它只是将它添加到你的URL中:"jtSearchParam =wü"但它实际上应该是这样的:"jtSearchParam = w%C3%BC"(如果它不是你会得到编码错误您使用IE时的服务器端)
因此,无论您在URL中添加某些值,请确保encodeURI在将该值实际添加到URL之前对该值使用JavaScript方法示例:
encodeURI(jtSearchParam)
| 归档时间: |
|
| 查看次数: |
5096 次 |
| 最近记录: |