2012-09-06 80 views
1

Recenlty我在服务器的JSON响应波兰字符有大问题。我有这个简单的Ajax请求:在服务器端春季mvc jquery ajax响应作为json编码问题

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服务器端和数据库好吧,没问题。 但是当涉及到读响应从服务器,它是:

??? 

代替波兰语炭等:

ąćź 

而且接收来自服务器的响应时,在发送用数据的请求是它不仅不能正确编码。

在我的web.xml中,我已经过滤了字符编码。

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

+0

确切重复http://stackoverflow.com/questions/12105617/spring-3-1-mvc-getting-character-encoding-error-while-using-responsebody-anno –

回答

1

现在我100%肯定是在服务器端和数据库encoidng从那里我从数据OK

尝试添加Content-Type头,如果它不存在INT您的回应:

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

确保在从数据库中读取时使用UTF-8字符集。杰克逊的编码默认为UTF-8,因此您的数据可能不会使用UTF-8进行编码?!?

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

1

试着改变你的反应类型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); 
} 
0

你可以使用Spring注解上述控制器类RequestMapping为receveing 应用/ 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 
} 
... 
}