最近在用Spring MVC做一个小的后台服务,遇到了ajax请求中的字符编码问题。
ajax请求如下:
function PostJSONQuery(json,on_success)
@H_502_4@
{
@H_502_4@
$.ajax({
@H_502_4@
url: "json.do",@H_502_4@
contentType : "application/json;charset=utf-8",@H_502_4@
processData : true,@H_502_4@
data: json,@H_502_4@
dataType: "json",@H_502_4@
success: function(response) {
@H_502_4@
on_success(response);
@H_502_4@
},@H_502_4@
error: function (xhr,ajaxOptions,thrownError) {
@H_502_4@
@H_502_4@
}
@H_502_4@
});
@H_502_4@
}
@H_502_4@
以上虽然设置了
charset=utf-8,但是好像没什么作用最后传到后台的还是?·?????·????,编码格式不对
@H_502_4@
function changeImagesAddress(){
@H_502_4@
alert("test");
@H_502_4@
var alert_query_json = {
@H_502_4@
effectname:"淡入淡出"
@H_502_4@
};
@H_502_4@
@H_502_4@
PostJSONQuery(alert_query_json,function(response){
@H_502_4@
alert("response");
@H_502_4@
alert(response.listInfo.length);//response 文件夹集合 response[i].value
@H_502_4@
//response[i].name
@H_502_4@
@H_502_4@
alert(response.listInfo[0].age);
@H_502_4@
@H_502_4@
});
@H_502_4@
}
@H_502_4@
一开始打算请求的中文字符转为utf-8,后来想想还是在服务端将字符转化为utf-8;
@H_502_4@
查资料知道/**http头默认以ISO-8859-1 格式传送中文字符**/ 所以在服务端将收到的字符进行重新编码
@H_502_4@
@RequestMapping(value="/jsp/changeImagesAddress")
@H_502_4@
@ResponseBody
@H_502_4@
public OneEffectImagesAddress validataUser(@RequestParam String effectname) throws UnsupportedEncodingException{
@H_502_4@
OneEffectImagesAddress infoList=null;
@H_502_4@
/**http头默认以ISO-8859-1 格式传送中文字符**/
@H_502_4@
String str_effectname=new String (effectname.getBytes("ISO-8859-1"),"utf-8");
@H_502_4@
infoList=DaoImpl.getOneEffectImagesAddress(str_effectname);
@H_502_4@
return infoList;
@H_502_4@
}
@H_502_4@
@H_502_4@
@H_502_4@
@H_502_4@
上面的方法是硬解码,不太灵活,tomcat服务器对web服务器是做
ISO-8859-1编码的,相对于修改前台的编码,在前台修改更加灵活
@H_502_4@
function PostJSONQuery(json,on_success)
@H_502_4@
{
@H_502_4@
$.ajax({
@H_502_4@
url: "json.do",@H_502_4@
type:"post",@H_502_4@
contentType : "application/json;charset=utf-8",@H_502_4@
processData : true,@H_502_4@
data: json,@H_502_4@
dataType: "json",@H_502_4@
success: function(response) {
@H_502_4@
on_success(response);
@H_502_4@
},@H_502_4@
error: function (xhr,thrownError) {
@H_502_4@
@H_502_4@
}
@H_502_4@
});
@H_502_4@
}上面的可以发现一个问题,
contentType : "application/json;charset=utf-8",之前我在xml里面配置了
@H_502_4@
@H_502_4@
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
@H_502_4@
<property name="mediaTypes">
@H_502_4@
<map>
@H_502_4@
<entry key="html" value="text/html"/>
@H_502_4@
<entry key="spring" value="text/html"/>
@H_502_4@
<entry key="json" value="application/json"/>
@H_502_4@
</map>
@H_502_4@
</property>
@H_502_4@
</bean>
@H_502_4@
@H_502_4@
这么一段,后来发现 这一段不需要也没关系。
@H_502_4@
contentType : "application/json;charset=utf-8"应该修改成
contentType : "application/x-www-form-urlencoded;charset=utf-8",
@H_502_4@
@H_502_4@
@H_502_4@
我的整个项目的统一编码utf-8.所以后台无需进行解码,直接可以得到中文字符。
@H_502_4@
@H_502_4@
@H_502_4@
spring mvc配置文件
@H_502_4@
<?xml version="1.0" encoding="UTF-8"?>
@H_502_4@
<beans xmlns="http://www.springframework.org/schema/beans"
@H_502_4@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@H_502_4@
xmlns:mvc="http://www.springframework.org/schema/mvc"
@H_502_4@
xmlns:p="http://www.springframework.org/schema/p"
@H_502_4@
xmlns:context="http://www.springframework.org/schema/context"
@H_502_4@
xmlns:aop="http://www.springframework.org/schema/aop"
@H_502_4@
xmlns:tx="http://www.springframework.org/schema/tx"
@H_502_4@
xsi:schemaLocation="http://www.springframework.org/schema/beans
@H_502_4@
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
@H_502_4@
http://www.springframework.org/schema/context
@H_502_4@
http://www.springframework.org/schema/context/spring-context-3.0.xsd
@H_502_4@
http://www.springframework.org/schema/aop
@H_502_4@
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
@H_502_4@
http://www.springframework.org/schema/tx
@H_502_4@
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
@H_502_4@
http://www.springframework.org/schema/mvc
@H_502_4@
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
@H_502_4@
http://www.springframework.org/schema/context
@H_502_4@
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
@H_502_4@
<!--
@H_502_4@
使Spring支持自动检测组件,如注解的Controller
@H_502_4@
-->
@H_502_4@
<context:component-scan base-package="com"/>
@H_502_4@
@H_502_4@
@H_502_4@
@H_502_4@
<bean id="viewResolver"
@H_502_4@
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
@H_502_4@
p:prefix="/jsp/"
@H_502_4@
p:suffix=".jsp" />
@H_502_4@
@H_502_4@
@H_502_4@
@H_502_4@
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
@H_502_4@
<property name="messageConverters">
@H_502_4@
<list >
@H_502_4@
<ref bean="mappingJacksonHttpMessageConverter" />
@H_502_4@
</list>
@H_502_4@
</property>
@H_502_4@
</bean>
@H_502_4@
@H_502_4@
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
@H_502_4@