您的位置:首页 > 其它

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);

}

}

});

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: