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
- ajax请求中遇到的中文字符编码问题
- Ajax向后台请求的数据中中文编码的问题
- ssh中jquery发送ajax请求遇到的问题
- JSF中请求响应的字符编码问题
- python 处理中文遇到的编码问题总结 以及 字符str的编码如何判断
- ajax请求中URL和参数的编码问题
- Python中文编码过程中遇到的一些问题
- python读取csv中所遇到的中文编码问题
- 【Java】编程过程中遇到的中文编码问题
- jquery,ajax开发中遇到的中文乱码问题
- [完]PHP文件下载 下载GET值中含有中文字符的文件,保存中文文件名,文件不能打开,遇到的编码问题
- Qt中关于中文字符编码问题
- ajax请求时遇到的问题
- Ajax请求Json遇到的问题总结
- Ajax请求传递参数遇到的问题
- 做爬虫时候遇到的中文字符编码问题(Python)
- Ajax发送请求的编码问题
- jquery ajax 请求gb2312编码的问题
- java学习之面试题5
- unity3d笔记(5)——调用Webcam设备
- Palindrome Partitioning
- jar not loaded. See Servlet Spec 2.3, section 9.7.2.
- DButils的使用感受
- ajax请求中遇到的中文字符编码问题
- java网络唤醒,win7的网络唤醒(后面更新Liunx的)
- log4j日志记录中遇到的问题
- XFire的使用
- Xfile 错误记录 object is not an instance of declaring class
- Spring MVC+Hibernate配置
- commons-httpclient简单使用
- MySQL uuid的使用
- Guava的简单使用