ajax请求中遇到的中文字符编码问题

来源:互联网 发布:淘宝优惠券小程序制作 编辑:程序博客网 时间:2024/05/19 12:14
最近在用Spring MVC做一个小的后台服务,遇到了ajax请求中的字符编码问题。
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;
}

上面的方法是硬解码,不太灵活,tomcat服务器对web服务器是做ISO-8859-1编码的,相对于修改前台的编码,在前台修改更加灵活
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, ajaxOptions, 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",

我的整个项目的统一编码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">
    <!--
        使Spring支持自动检测组件,如注解的Controller
    -->
    <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>
将字符进行重新编码后,再进行查询。这样就可以避免编码格式不正确的问题。还有其他方法尝试待补充。

0 0
原创粉丝点击