XMLHTTP提交中文出现乱码问题解决方案
xwing@263.net
客户端代码
function submitSC(){
try{
var myxml = new ActiveXObject("MSXML2.DOMDocument");
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
myxml.async = false;
myxml.loadXML(scSCData.xml);
xmlHttp.open("POST", "<%=getAspURL("listSC.asp")%>sinkSCData.asp" , false);
//xmlHttp.setRequestHeader("CONTENT-TYPE","text/xml; charset = gb2312");
xmlHttp.send(myxml);
alert(xmlHttp.responseText);
}
catch (exception){
var errDes = '无法提交数据。<br>详细错误信息:"' + exception.description + '"。'
window.location.href="err.asp?errtype="+errDes; }
}
<%
function getAspURL(aspfileName)
getAspURL = "http://" &_
request.servervariables("http_host") &_
left(request.servervariables("path_info"),len(request.servervariables("path_info"))-len(aspFileName))
end function
%>
服务器端代码
方案一:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Response.ContentType = "text/xml"
Response.CharSet = "GB2312"
if Request.ServerVariables("REQUEST_METHOD") = "POST" then
dim fname,xmldoc, xmlhead
Set xmldoc = server.CreateObject("Msxml2.DOMDocument")
set xmlhead = xmldoc.createProcessingInstruction("xml","version=""1.0"" encoding=""GB2312""")
xmldoc.load(Request)
xmldoc.removeChild xmldoc.firstChild
xmldoc.insertBefore xmlhead,xmldoc.firstChild
fname = Server.MapPath(".") & "/recv.xml"
xmldoc.save(fname)
xmldoc.save(response) '回写客户端测试
end if
%>
方案二:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Response.ContentType = "text/xml"
Response.CharSet = "GB2312"
if Request.ServerVariables("REQUEST_METHOD") = "POST" then
dim fname,xmldoc,aStream
Set xmldoc = server.CreateObject("Msxml2.DOMDocument")
set aStream = server.createobject("adodb.stream")
xmldoc.load(Request)
astream.mode = 2 'writeMode
astream.type = 2 '文本模式
astream.open
astream.charset = "gb2312"
astream.writetext "<?xml version=""1.0"" encoding=""GB2312""?>" & xmldoc.documentElement.xml
fname = Server.MapPath(".") & "/recv.xml"
astream.savetofile fname, 2
astream.close
'回写客户端测试
Response.Write "<?xml version=""1.0"" encoding=""GB2312""?>" & xmldoc.documentElement.xml
end if
%>