jquery - Spring: Ajax call to @ResponseBody param encoding fails in IE -
i'm working spring , try make ajax call @responsebody in controller.
update
okay, added changes got told to ajax settings. param "jtsearchparam" still has same encoding problem in ie. + got other error, 406, response header has wrong content-type.
here's new code
controller:
@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; }
so see set content type in produces
doesn't help. if debug response header looks this: (that causes 406 not acceptable browser)
and new ajax settings:
... 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' ...
and parameters still same in ie!
okay problem json content-type can solved this:
with responseentity you're able change content-type of response header, way ajax can interpret json object correct way , wont 406 http-error.
@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); }
the problem encoding can solved this:
ie won't encode "ü, ä, etc." correctly, it'll add url this:"jtsearchparam=wü" should that:"jtsearchparam=w%c3%bc" (if doesn't you'll encoding errors on serverside when use ie)
so ever add values url, make sure use javascript method encodeuri
on value before add url example:
encodeuri(jtsearchparam)