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