spring mvc jquery ajax响应为json编码问题 [英] spring mvc jquery ajax response as json encoding issue

查看:81
本文介绍了spring mvc jquery ajax响应为json编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谴责我对来自服务器的JSON响应中的波兰语字符有很大的疑问.我对此有一个简单的Ajax请求:

Recenlty I have big problem with Polish Characters in JSON response from the server. I have simple Ajax request for this:

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
    } );

以及服务器端的相应控制器:

And appropriate Controller at server end:

@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没问题,这没问题. 但是当涉及到从服务器读取响应时,它是:

Now I'm 100% sure that encoidng on server side and database from where I take data from is OK, no problem with that. But when It comes to reading response from server it is:

???

代替波兰字符,例如:

ąćź

此外,仅当接收到来自服务器的响应并且发送带有数据的请求时,编码才正确.

Moreover it only fails when receiving response from server, while sending a request with data is encoded correctly.

在我的web.xml中,我具有用于字符编码的过滤器.

In my web.xml I have filter for character encoding.

对此有任何帮助吗?我没主意了.

Any help with this? I'm out of ideas.

推荐答案

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

Try changing your response type to org.springframework.http.ResponseEntity

public ResponseEntity<String> 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<String>(allyourjson, responseHeaders, HttpStatus.OK);
}

这篇关于spring mvc jquery ajax响应为json编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆