在使用jQuery的ajax发送http请求时,经常会遇到中文乱码的情况。下面的方法将
$.ajax({
type: "POST",
url: url,
data: data,
dataType: "json",
success: function(param){
if(param.data === "success"){
alert("添加成功!");
}else {
alert("ERROR");
}
}
});
后台收到的数据:
"name=%E6%B5%8B%E8%AF%95&phone=123456789&content=%E6%B5%8B%E8%AF%95"
使用fastjson的jar工具包
import com.alibaba.fastjson.JSON;
......
request.setCharacterEncoding("utf-8");
BufferedReader reader=request.getReader();
String params=reader.readLine();
//使用工具包将发送的数据转化为json字符串
String json=JSON.toJSONString(params);
//使用java.net包下的URLDecoder的解码类解码
import java.net.URLDecoder;
json=URLDecoder.decode(json);
//得到中文字符
"name=娴嬭瘯&phone=123456789&content=娴嬭瘯"
//但此时还是乱码,因为解码字符不对应
json=URLDecoder.decode(json,"utf-8");
"nickname=你&phone=123&content=测试"
//在通过对字符串的解析取出相应的字段即可
你
2022-2-23 13:16:0
111
你
. . .
相关推荐
热门推荐
gitHub - git 回滚
29天前
xml mysql 强制走区分大小写查询
28天前
Axure RP 9基础教程组件基础1
28天前
QT在QLabel上实现超链接
28天前
ifconfig 参数解释
25天前
ads via 小工具