js调用AJAX时Get和post的乱码解决方法
更新时间:2013年06月04日 16:35:51 作者:
在使用"get"时,抓取的页面最后加上编码类型,在使用post时用vbscript解决了编码问题,具体实现如下,有类似情况的朋友可以参考下哈
在使用"get"时,抓取的页面最后加上编码类型
<%
服务器端:servletactioncontext.getresponse().setcharacterencoding("utf-8");
客户端 网页特效p/jsp.html target=_blank >jsp教程: <%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
response.expires = -9999
response.addheader "pragma","no-cache"
response.addheader "cache-ctrol","no-cache"
response.addheader "content-type","text/html; charset=gb2312"'这是重点,否则会出现乱码
response.write "中文汉字"%>
2、在使用post时用vbscript解决了编码问题:
源码如下:
<script language="vbscript">
function urlencoding(vstrin)
strreturn = ""
for i = 1 to len(vstrin)
thischr = mid(vstrin,i,1)
if abs(asc(thischr)) < &hff then
strreturn = strreturn & thischr
else
innercode = asc(thischr)
if innercode < 0 then
innercode = innercode + &h10000
end if
hight8 = (innercode and &hff00) &hff
low8 = innercode and &hff
strreturn = strreturn & "%" & hex(hight8) & "%" & hex(low8)
end if
next
urlencoding = strreturn
end function
function bytes2bstr(vin)
strreturn = ""
for i = 1 to lenb(vin)
thischarcode = ascb(midb(vin,i,1))
if thischarcode < &h80 then
strreturn = strreturn & chr(thischarcode)
else
nextcharcode = ascb(midb(vin,i+1,1))
strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode))
i = i + 1
end if
next
bytes2bstr = strreturn
end function
</script>
下面是使用vbscript函数:
<script language=网页特效>
/**
* 初始化一个xmlhttp对象
*/
function initajax()
{
var ajax=false;
try {
ajax = new activexobject("msxml2.xmlhttp");
} catch (e) {
try {
ajax = new activexobject("microsoft.xmlhttp");
} catch (e) {
ajax = false;
}
}
if (!ajax && typeof xmlhttprequest!='undefined') {
ajax = new xmlhttprequest();
}
return ajax;
}
function saveuserinfo()
{
var msg = document.getelementbyid("msg");
var f = document.user_info;
var username = f.user_name.value;
var userage = f.user_age.value;
var usersex = f.user_sex.value;
var url = "save.asp教程";
var poststr = urlencoding("user_name="+ username +"&user_age="+ userage +"&user_sex="+ usersex);//post时采用编码传递
var ajax = initajax();
ajax.open("post", url, true);
ajax.setrequestheader("content-type","application/x-www-form-urlencoded");
ajax.send(poststr);
ajax.onreadystatechange = function() {
if (ajax.readystate == 4 && ajax.status == 200) {
msg.innerhtml = bytes2bstr(ajax.responsebody); //获取时解码
}
}
}
</script>
<form name="user_info">
姓名:<textarea name="user_name" /></textarea><br />
年龄:<input type="text" name="user_age" /><br />
性别:<input type="text" name="user_sex" /><br />
<input type="button" value="提交表单" onclick="saveuserinfo()">
</form>
<div id="msg"></div>
成功
复制代码 代码如下:
<%
服务器端:servletactioncontext.getresponse().setcharacterencoding("utf-8");
客户端 网页特效p/jsp.html target=_blank >jsp教程: <%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
response.expires = -9999
response.addheader "pragma","no-cache"
response.addheader "cache-ctrol","no-cache"
response.addheader "content-type","text/html; charset=gb2312"'这是重点,否则会出现乱码
response.write "中文汉字"%>
2、在使用post时用vbscript解决了编码问题:
源码如下:
复制代码 代码如下:
<script language="vbscript">
function urlencoding(vstrin)
strreturn = ""
for i = 1 to len(vstrin)
thischr = mid(vstrin,i,1)
if abs(asc(thischr)) < &hff then
strreturn = strreturn & thischr
else
innercode = asc(thischr)
if innercode < 0 then
innercode = innercode + &h10000
end if
hight8 = (innercode and &hff00) &hff
low8 = innercode and &hff
strreturn = strreturn & "%" & hex(hight8) & "%" & hex(low8)
end if
next
urlencoding = strreturn
end function
function bytes2bstr(vin)
strreturn = ""
for i = 1 to lenb(vin)
thischarcode = ascb(midb(vin,i,1))
if thischarcode < &h80 then
strreturn = strreturn & chr(thischarcode)
else
nextcharcode = ascb(midb(vin,i+1,1))
strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode))
i = i + 1
end if
next
bytes2bstr = strreturn
end function
</script>
下面是使用vbscript函数:
复制代码 代码如下:
<script language=网页特效>
/**
* 初始化一个xmlhttp对象
*/
function initajax()
{
var ajax=false;
try {
ajax = new activexobject("msxml2.xmlhttp");
} catch (e) {
try {
ajax = new activexobject("microsoft.xmlhttp");
} catch (e) {
ajax = false;
}
}
if (!ajax && typeof xmlhttprequest!='undefined') {
ajax = new xmlhttprequest();
}
return ajax;
}
function saveuserinfo()
{
var msg = document.getelementbyid("msg");
var f = document.user_info;
var username = f.user_name.value;
var userage = f.user_age.value;
var usersex = f.user_sex.value;
var url = "save.asp教程";
var poststr = urlencoding("user_name="+ username +"&user_age="+ userage +"&user_sex="+ usersex);//post时采用编码传递
var ajax = initajax();
ajax.open("post", url, true);
ajax.setrequestheader("content-type","application/x-www-form-urlencoded");
ajax.send(poststr);
ajax.onreadystatechange = function() {
if (ajax.readystate == 4 && ajax.status == 200) {
msg.innerhtml = bytes2bstr(ajax.responsebody); //获取时解码
}
}
}
</script>
<form name="user_info">
姓名:<textarea name="user_name" /></textarea><br />
年龄:<input type="text" name="user_age" /><br />
性别:<input type="text" name="user_sex" /><br />
<input type="button" value="提交表单" onclick="saveuserinfo()">
</form>
<div id="msg"></div>
成功
相关文章
自己实现string的substring方法 人民币小写转大写,数字反转,正则优化
这是最近碰到的几个题目,简单地写一下我自己的方案,在此分享给大家,也希望能看到大家的方案2012-09-09js中动态创建json,动态为json添加属性、属性值的实例
下面小编就为大家带来一篇js中动态创建json,动态为json添加属性、属性值的实例。小编觉的挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-12-12javascript获取所有同类checkbox选项(实例代码)
javascript获取所有同类checkbox选项的简单实例。需要的朋友可以过来参考下,希望对大家有所帮助2013-11-11
最新评论