Ajax 请求返回字符串乱码问题:
js://获取接收人名称串suggest.getUserNames=function(ids){alert(1);$.ajax({type:"GET",url:getRo...
js:
//获取接收人名称串
suggest.getUserNames = function(ids){
alert(1);
$.ajax({
type : "GET",
url : getRootPath()+"/ic/suggest/getUserNames.action",
contentType: "application/json;charset=GBK",
data : {"ids": ids},
//dataType : "json",
success : function(data) {
alert(data);
select2InitEchoToPerson("suggestForm #recipientIds-recipientIds",eval(data));
}
})
};
后台拼串:
public String getUserNames(String ids) throws Exception{
//根据用户id拼接的字符串去查找对应的用户名称,然后拼成字符串返回到实体类中带入页面
StringBuffer recipientNames = new StringBuffer("[");
List<User> userList = userService.queryUserByIds(ids);
if(userList != null){
for (User user : userList) {
recipientNames.append("{id:").append(user.getId())
.append(",");
recipientNames.append("text:\"")
.append(user.getDisplayName()).append("\"}");
recipientNames.append(",");
}
if (recipientNames.length() > 0 && recipientNames.charAt(recipientNames.length() - 1) == ',') {
recipientNames.deleteCharAt(recipientNames.length() - 1);
recipientNames.append("]");
}
}
return recipientNames.toString();
}
返回的data为[{id:14,text:???}] 乱码了,怎么解决
UTF-8 也试了 还是乱码 展开
//获取接收人名称串
suggest.getUserNames = function(ids){
alert(1);
$.ajax({
type : "GET",
url : getRootPath()+"/ic/suggest/getUserNames.action",
contentType: "application/json;charset=GBK",
data : {"ids": ids},
//dataType : "json",
success : function(data) {
alert(data);
select2InitEchoToPerson("suggestForm #recipientIds-recipientIds",eval(data));
}
})
};
后台拼串:
public String getUserNames(String ids) throws Exception{
//根据用户id拼接的字符串去查找对应的用户名称,然后拼成字符串返回到实体类中带入页面
StringBuffer recipientNames = new StringBuffer("[");
List<User> userList = userService.queryUserByIds(ids);
if(userList != null){
for (User user : userList) {
recipientNames.append("{id:").append(user.getId())
.append(",");
recipientNames.append("text:\"")
.append(user.getDisplayName()).append("\"}");
recipientNames.append(",");
}
if (recipientNames.length() > 0 && recipientNames.charAt(recipientNames.length() - 1) == ',') {
recipientNames.deleteCharAt(recipientNames.length() - 1);
recipientNames.append("]");
}
}
return recipientNames.toString();
}
返回的data为[{id:14,text:???}] 乱码了,怎么解决
UTF-8 也试了 还是乱码 展开
2个回答
展开全部
application/json;charset=UTF-8
追问
不好用啊。。。试过了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在后台请求的第一行加上这句"response.setCharacterEncoding("utf-8")",但是通常的情况是在web.xml中加字符过滤器,
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
希望能这两个方法能帮到你,有什么问题,欢迎交流。
追问
后台用的springMVC注解的形式,没有response的
追答
你把HttpServletResponse response当成参数传到方法里不就行了,例如:
@RequestMapping(value="testRequest")
public String businessShop(HttpServletResponse response, HttpServletRequest request){
response.setCharacterEncoding("utf-8");
//业务逻辑(这个response是直接能用的)
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询