Ajax乱码解决
2015-09-06 17:33
183 查看
ajax乱码解决几种方案
1.页面出现乱码:
2.服务器出现乱码
3.写原生的ajax需要二次编码 post=encodeURI(post); post=encodeURI(post);写两遍,jquery不用
4.jquery的ajax调用处 加不加 content-type的参数
都不影响。contentType: "application/x-www-form-urlencoded; charset=utf-8",
服务器端例子:
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
//HttpSession session = request.getSession(false);
//UserInfor userInfor = (UserInfor) session.getAttribute("userInfor");
try {
request.setCharacterEncoding("utf-8");//解决服务器出现乱码问题
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setContentType("text/html;charset=utf-8");//解决页面出现乱码问题
String jsh = request.getParameter("changejsh");
KctzLogic kctzLogic = new KctzLogic();
String jsmandzcsm = kctzLogic.queryjsxx(jsh);
PrintWriter os;
try {
os = response.getWriter();
os.print(jsmandzcsm);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
页面端例子(jquery):
function qd(){
var changejsh = $("#zjjszh").val();
var jsm="";
$.ajax({
type: "POST",
url: "<%=request.getContextPath()%>/changejs.do?",
data: "changejsh='"+changejsh+"'",
success: function(data){
if(data!=null){
data = data.split(',');
jsm = data[0];
zcsm = data[1];
alert(jsm);
alert(zcsm);
}
}
});
}
1.页面出现乱码:
2.服务器出现乱码
3.写原生的ajax需要二次编码 post=encodeURI(post); post=encodeURI(post);写两遍,jquery不用
4.jquery的ajax调用处 加不加 content-type的参数
都不影响。contentType: "application/x-www-form-urlencoded; charset=utf-8",
服务器端例子:
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
//HttpSession session = request.getSession(false);
//UserInfor userInfor = (UserInfor) session.getAttribute("userInfor");
try {
request.setCharacterEncoding("utf-8");//解决服务器出现乱码问题
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setContentType("text/html;charset=utf-8");//解决页面出现乱码问题
String jsh = request.getParameter("changejsh");
KctzLogic kctzLogic = new KctzLogic();
String jsmandzcsm = kctzLogic.queryjsxx(jsh);
PrintWriter os;
try {
os = response.getWriter();
os.print(jsmandzcsm);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
页面端例子(jquery):
function qd(){
var changejsh = $("#zjjszh").val();
var jsm="";
$.ajax({
type: "POST",
url: "<%=request.getContextPath()%>/changejs.do?",
data: "changejsh='"+changejsh+"'",
success: function(data){
if(data!=null){
data = data.split(',');
jsm = data[0];
zcsm = data[1];
alert(jsm);
alert(zcsm);
}
}
});
}
相关文章推荐
- SecureCRT 连接 Ubuntu
- .NET String.Format 方法 线程安全问题
- IOS第18天(9,核心动画-动画组)
- 颜色“秀”
- Python编译器的选择与比较(入门必备)
- Qt Quick中的信号机制
- 数据库笔试题
- iOS--设置Launch Image 启动图片
- 微信公众号支付Java DEMO
- 面向对象学习
- 线段树小结
- Java Static代码块与构造方法初始化顺序
- XCode中使用github做多人协同开发
- 如何配置一个软件系统的总体样式(用qss文件)
- ZOJ 2868 Incredible Cows
- gradle多渠道打包及友盟统计-eclipse版本
- IOS获取设备的UUID
- [置顶] 程序转制作人——游戏设计课程导论
- 安装mysql报curses的解决
- 解决ListView嵌套在ScrollerView中显示不全以及滑动冲突的问题