通过在后台用UTF8转码的方式可以解决中文乱码问题。这种方法针对form表单提交,编码在服务器完成。代码如下: | |
if(window.XMLHttpRequest) { //Mozilla 浏览器 http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) {//设置MiME类别 http_request.overrideMimeType('text/xml'); } } else if (window.ActiveXObject) { // IE浏览器 try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { // 异常,创建对象实例失败 window.alert("不能创建XMLHttpRequest对象实例."); return false; } http_request.onreadystatechange = processRequest; // 确定发送请求的方式和URL以及是否同步执行下段代码 http_request.open("POST", url, true); http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); http_request.send(poststr); } // 处理返回信息的函数 function processRequest() { if (http_request.readyState == 4) { // 判断对象状态 if (http_request.status == 200) { // 信息已经成功返回,开始处理信息 alert(http_request.responseText); } else { //页面不正常 alert("您所请求的页面有异常。"); } } } function form_submit() { var f = document.form1; var username = f.username.value; var password = f.password.value; var poststr = ""; if(username=="") { window.alert("用户名不能为空。"); f.username.focus(); return false; } if(password=="") { window.alert("密码不能为空。"); f.password.focus(); return false; } poststr = "username="+encodeURI(f.username.value)+"&password="+f.password.value; send_request('form_handle.jsp',poststr); return false; } </script> </head> <body> |
Ajax开发过程中提交获取数据的乱码问题
最新推荐文章于 2023-11-15 16:23:01 发布