大佬教程收集整理的这篇文章主要介绍了jquery – Spring:在IE中对@ResponseBody param编码的Ajax调用失败,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,我把我告诉的更改添加到了我的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 @R_78_10586@lRecordCount = userDao.getamountOfRows(jtSearchParam); usersJsonString = "{\"message\":null,\"Result\":\"OK\",\"Records\":" + usersJsonString + ",\"@R_78_10586@lRecordCount\":" + @R_78_10586@lRecordCount + "}"; JSONObject usersJsonObject = new JSONObject(usersJsonString); return usersJsonObject; }
所以当你看到我在产品中设置内容类型但这没有帮助.
如果我调试响应头它看起来像这样:
(这会导致浏览器无法接受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' ...
我的参数在IE中看起来仍然一样!
使用ResponseEntity,您可以更改响应头的内容类型,这样ajax可以正确解释json对象,并且您不会得到406 http错误.
@requestMapping(method = requestMethod.POST) public ResponseEntity<String> getUsers(@requestParam int jtStarTindex,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,jtSearchParam); Type userListType = new TypeToken<List<User>>() {}.getType(); String usersJsonString = gson.toJson(users,userListTypE); int @R_78_10586@lRecordCount = userDao.getamountOfRows(jtSearchParam); usersJsonString = "{\"message\":null,\"@R_78_10586@lRecordCount\":" + @R_78_10586@lRecordCount + "}"; return new ResponseEntity<String>(usersJsonString,responseHeaders,httpStatus.OK); }
编码问题可以像这样解决:
IE不会编码你的“ü,ä等”正确的,它只是将它添加到您的URL,如下所示:“jtSearchParam =wü”但它实际上应该是这样的:“jtSearchParam =wü”(如果不是这样,当你在服务器端时,你会得到编码错误使用IE)
因此,无论您在URL中添加某些值,请确保在将该值实际添加到URL之前对该值使用JavaScript方法encodeURI
例:
是encodeURI(jtSearchParam)
以上是大佬教程为你收集整理的jquery – Spring:在IE中对@ResponseBody param编码的Ajax调用失败全部内容,希望文章能够帮你解决jquery – Spring:在IE中对@ResponseBody param编码的Ajax调用失败所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。