ajax向服务器Post的数据如何解决乱码问题?高分求解!!!!!!急!!!!!!
刚学AJAX就遇到大问题:
利用ajax 向服务器post的表单数据在存入数据库后都是乱码.想了一下午都没解决,快要对ajax没信心了.请大家帮忙解决,不胜感激。也在网上找了好多相关文章,但都是点到为止,没给出确切的解决方案。我们菜鸟根本就看不懂。我的页面都设置了gb2312,看网上说POST的数据都是utf-8编码的。这样一来,如果有中文的话post到服务器就全是乱码。我这里post的数据包含title和content两个内容("title="+titleValue+"&content="+contentValue),如果是乱码的话,首先在服务器端用request("title")和request("content")有可能就会读出问题,request("title")会把整个字符串都进去,比如:“涓€浜?content=涓€宸”,而request("content")则读不到值。这样一来,想利用转码函数对两个变量分别进行转码也没办法了,有没有方法先把post过来的整个字符串先进行转码?。其次,就算不出前面的问题,那么又如何转码呢?用了网上那个asp 的gb2utf 和utf2gb的转码函数都不行,存到数据库的还是乱码。
实在是搞得没办法了。求助大家。如能解决,不胜感激。!!!!
源码如下(是ASP的):
第一个文件:ArcitleAdd.asp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<title>标签</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
<!--
/* CSS Tabs */
#navlist {
padding: 3px 0;
margin-left: 0;
border-bottom: 1px solid #778;
font: bold 12px "宋体";
}
#navlist li {
list-style: none;
margin: 0;
display: inline;
padding: 3px 0.5em;
margin-left: 3px;
border: 1px solid #778;
border-bottom: none;
background: #DDE;
text-decoration: none;
cursor: pointer;
}
#navlist #current {
background: white;
border-bottom: 1px solid white;
cursor:default;
}
/*-----------*/
#alarm {
background-color: #FFFFCC;
border: 1px dashed #FFCC00;
width:90%;
visibility: hidden;
}
/*-----------*/
#layer1 {
margin-top:0;
padding-top:0;
border-top-width: 0px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: none;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #778;
border-right-color: #778;
border-bottom-color:#778;
border-left-color: #778;
}
/*ArticleAdd Form css-----*/
label
{
float: left;
width: 40px;
font-weight: bold;
padding-left:20px;
}
input
{
margin-bottom: 3px;
}
.error
{
margin-left: 80px;
color: #ff0000;
}
#myform span {
background-color: #FFFF99;
border: 1px solid #FF9900;
margin-left: 60px;
cursor:pointer;
}
-->
</style>
<script type="text/javascript" language="javascript">
var http_request = false;
var cache = new Array();
//设置当前标签
function makeSign(url,sign) {
if(sign.id!="current"){
if(document.getElementById("current")){
document.getElementById("current").id="";
}
sign.id="current";
}
getAJAX(url,"showsign");
}
//开始AJAX
function getAJAX(url,action) {
http_request = false;
//alert (action);
if (action=="articleadd"){
titleValue=encodeURIComponent(document.myform.title.value);
contentValue=encodeURIComponent(document.myform.content.value);
var cacheEntry="title="+titleValue+"&content="+contentValue;
alert (cacheEntry);
}
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
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) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST',url, true);
//处理标签点击
if (action=="showsign"){
http_request.send(null);
}
//处理提交文章添加表单
if (action=="articleadd"){
//alert(cacheEntry);
http_request.setRequestHeader("content-length",cacheEntry.length);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //如果要用send的话就不能少这一句.
http_request.send(cacheEntry);
}
}
function alertContents() {
if(http_request.readyState < 4){
document.getElementById ('layer1').innerHTML = "正在处理中...";
}
if (http_request.readyState == 4) {
if (http_request.status == 200) {
document.getElementById ('layer1').innerHTML = http_request.responseText;
} else {
document.getElementById ('alarm').style.visibility="visible";
}
}
}
function randomNumber(limit){
return Math.floor(Math.random()*limit);
}
</script>
</head>
<!-- 在body中设定默认当前标签-->
<body onload="makeSign('ArticleAction.asp?Show=ArticleAdd&num='+randomNumber(1000000)+'',document.getElementById('current'));">
<div id="alarm"></div>
<div id="navlist">
<!-- CSS Tabs -->
<li id="current" onClick="makeSign('ArticleAction.asp?Show=ArticleAdd&num='+randomNumber(1000000)+'',this);">添加文章</li>
<li onClick="makeSign('ArticleAction.asp?Show=ArticleManage&num='+randomNumber(1000000)+'',this);">管理文章</li>
</div>
<fieldset id="layer1"></fieldset>
</body>
</html>
--------------------------------------------------------------
第二个文件:ArticleAction.asp,服务器端处理数据的文件
--------------------------------------------------------------
<% Response.Charset="GB2312" %>
<!-- #include file="../inc/conn.asp"-->
<%
dim Show,num
Show=trim(request("Show"))
num=trim(request("num"))
select case show
case "ArticleAdd"
ArticleAdd()
Case "ArticleManage"
ArticleManage()
Case "Add"
Add()
Case else
ArticleAdd()
end select
Sub ArticleAdd()
%>
<br />
<form method="post" name="myform" id="myform">
<label for="title">标题</label>
<input name="title" type="text" id="title"/>
<span id="titleFailed" class="error"></span>
<br />
<label for="content">内容</label>
<textarea name="content" cols="100%" rows="20" id="content"></textarea>
<span id="contentFailed" class="error"></span>
<br />
<br />
<span onClick="getAJAX('ArticleAction.asp?show=Add&num='+randomNumber(1000000)+'','articleadd');">提 交</span>
</form>
<%
end sub
Sub ArticleManage()
%>
<table width="100%" border="0" bordercolor="#993399">
<tr>
<td height="119">这是修改界面<%= num %></td>
<td> </td>
</tr>
</table>
<%
end sub
sub Add()
dim title,content,rs,sql
title=request("title")
content=request("content")
sql="select * from Article"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
rs.addnew
rs("title")=title
rs("content")=content
rs.update
rs.close
set rs=nothing
call closeConn()
response.write "文章保存成功!"
end sub
%>
问题点数:100、回复次数:14Top
1 楼fangzhe()回复于 2006-06-03 20:05:47 得分 15
XMLHttpRequest必然是UTF-8,而且通过Javascript转码根本不可能
试试<% Response.Charset="GB2312" %>改成UTF-8,不行就把网页都改成UTF-8的Top
2 楼jiangtao088(不够专业)回复于 2006-06-04 00:05:59 得分 15
我开始也遇到,不过那是我用的是PHP在服务器端用函数转了一下码就一切正常了,没怎么费事所以经验也不多。
但是解决乱码不外乎也就这几中方法,最简单的是把网页的编码也改成UTF-8,还有就是在服务器端进行编码转换等。Top
3 楼fangzhe()回复于 2006-06-04 11:45:27 得分 5
反正Javascript/XMLHttpRequest是转不了滴
只能从别的地方下手Top
4 楼ysqy6666(方来)回复于 2006-06-04 19:28:06 得分 0
我也想过把网页都改成UTF-8的,但是好像改成这个编码后,网页中原本的中文字又会变成乱码.真是没办法...希望谁能发个这方面的例子能提供参考一下.不胜感激!~~~Top
5 楼fangzhe()回复于 2006-06-04 21:14:34 得分 40
但是好像改成这个编码后,网页中原本的中文字又会变成乱码
怎么会呢?
用个批量编码转换程序就可以了啊,注意修改content-type的encodingTop
6 楼bingke111888(冰客)回复于 2006-06-04 21:37:27 得分 5
jiangtao088(不够专业):我开始也遇到,不过那是我用的是PHP在服务器端用函数转了一下码就一切正常了
请问在php中转换的函数是什么?Top
7 楼snmr_com(麒麟厍人)回复于 2006-06-04 22:12:28 得分 5
iconvTop
8 楼whxleem(feeling)回复于 2006-06-05 08:45:03 得分 5
public static String toChinese(String str) {
String a = null;
try {
if (str == null) {
return null;
} else {
a = new String(str.getBytes("ISO8859_1"), "GBK");
return a;
}
} catch (Exception e) {
return null;
}
}
在服务器端拿前台传过来的值得时候转化一下就行了Top
9 楼bingke111888(冰客)回复于 2006-06-05 13:33:47 得分 0
我用php为什么装不上iconv模块?Top
10 楼bingke111888(冰客)回复于 2006-06-05 16:34:53 得分 0
经过努力装上了,呵呵Top
11 楼deamonchan(陨落の星)回复于 2006-06-05 17:03:05 得分 5
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
解决~
Top
12 楼TSD(智之选,商欲达--智商购物系统zhishop.com)回复于 2006-06-06 11:50:48 得分 0
如果提交的内容很长会不会被截断啊?Top
13 楼the_rising_sun(初升的太阳)回复于 2006-06-08 09:36:11 得分 5
刚刚解决.
在服务器端接收时
request.setCharacterEncoding("UTF-8");
就正常了.
因为客户端是以UTF-8编码发送的,所以接收之前要设置成该编码才能正常显示.
Top
14 楼ysqy6666(方来)回复于 2006-06-18 20:47:42 得分 0
根据大家的回复总算解决了.
总结一下:
1、全部网页用utf-8编码进行保存。如果不是则可以用批量转换工具进行转换(到网上搜得到这个软件),转换后,原来文件中的一些中文会变成乱码,改过来就可以了。
2、将所有页面的编码标注改为utf-8.如果是Html则在<head>区域加入:<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
asp文件中加入:<% Response.Charset="utf-8" %>
经以上处理后,不会出现乱码问题,也不需要对传递的内容进行任何转码。Top




