spring mvc jquery ajax响应为json编码问题

Recenlty我在服务器的JSON响应中遇到波兰字符的大问题。 我有简单的Ajax请求:

jQuery.ajax( "/GetSimpleRuleList", { type:"GET", responseType:"application/json;charset=utf-8", contentType:"application/json;charset=utf-8", cache:false } ).done( function ( data ) { console.log( data ); //nevermind here } ); 

服务器端的适当控制器:

 @RequestMapping(value = "/GetSimpleRuleList", method = RequestMethod.GET) public @ResponseBody String getRuleList( ServletResponse response ) { //magically getting my list here response.setCharacterEncoding( "UTF-8" ); return //Using JACKSON ObjectWriter here } 

现在我100%确定服务器端和数据库中的encoidng从中获取数据是好的,没问题。 但是当谈到从服务器读取响应时,它是:

 ??? 

而不是波兰语char:

 ąćź 

此外,它仅在从服务器接收响应时失败,而发送具有数据的请求被正确编码。

在我的web.xml中,我有过滤字符编码。

对此有何帮助? 我没有想法。

现在我100%确定服务器端和数据库中的数据从中获取数据是可以的

尝试添加Content-Type标头(如果它尚未出现在您的响应中):

 response.setHeader("Content-Type", "application/json;charset=UTF-8") 

从数据库中读取时,请务必使用UTF-8字符集。 Jackson的编码默认为UTF-8,因此您的数据可能无法使用UTF-8编码?!?

从数据库读取时使用什么编码? 也许ISO-8859-2?

尝试将您的响应类型更改为org.springframework.http.ResponseEntity

 public ResponseEntity getRuleList(){ HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add("Content-Type", "application/json; charset=utf-8"); responseHeaders.setCacheControl("no-cache, max-age=0"); String allyourjson = "yourjsongoeshere"; return new ResponseEntity(allyourjson, responseHeaders, HttpStatus.OK); } 

您可以在控制器类上方使用spring注释RequestMapping来接收所有响应中的application / json; utf-8

 @Controller @RequestMapping(produces = {"application/json; charset=UTF-8","*/*;charset=UTF-8"}) public class MyController{ ... @RequestMapping(value = "/GetSimpleRuleList", method = RequestMethod.GET) public @ResponseBody String getRuleList( ServletResponse response ) { //magically getting my list here response.setCharacterEncoding( "UTF-8" ); return //Using JACKSON ObjectWriter here } ... } 
    Interesting Posts