js调用AJAX时Get和post的乱码解决方法
2023-06-13 09:15:00 时间
在使用"get"时,抓取的页面最后加上编码类型
复制代码代码如下:
<%
服务器端:servletactioncontext.getresponse().setcharacterencoding("utf-8");
客户端网页特效p/jsp.htmltarget=_blank>jsp教程:<%@pagelanguage="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解决了编码问题:
源码如下:
<scriptlanguage="vbscript">
functionurlencoding(vstrin)
strreturn=""
fori=1tolen(vstrin)
thischr=mid(vstrin,i,1)
ifabs(asc(thischr))<&hffthen
strreturn=strreturn&thischr
else
innercode=asc(thischr)
ifinnercode<0then
innercode=innercode+&h10000
endif
hight8=(innercodeand&hff00)&hff
low8=innercodeand&hff
strreturn=strreturn&"%"&hex(hight8)&"%"&hex(low8)
endif
next
urlencoding=strreturn
endfunction
functionbytes2bstr(vin)
strreturn=""
fori=1tolenb(vin)
thischarcode=ascb(midb(vin,i,1))
ifthischarcode<&h80then
strreturn=strreturn&chr(thischarcode)
else
nextcharcode=ascb(midb(vin,i+1,1))
strreturn=strreturn&chr(clng(thischarcode)*&h100+cint(nextcharcode))
i=i+1
endif
next
bytes2bstr=strreturn
endfunction
</script>
下面是使用vbscript函数:
<scriptlanguage=网页特效>
/**
*初始化一个xmlhttp对象
*/
functioninitajax()
{
varajax=false;
try{
ajax=newactivexobject("msxml2.xmlhttp");
}catch(e){
try{
ajax=newactivexobject("microsoft.xmlhttp");
}catch(e){
ajax=false;
}
}
if(!ajax&&typeofxmlhttprequest!="undefined"){
ajax=newxmlhttprequest();
}
returnajax;
}
functionsaveuserinfo()
{
varmsg=document.getelementbyid("msg");
varf=document.user_info;
varusername=f.user_name.value;
varuserage=f.user_age.value;
varusersex=f.user_sex.value;
varurl="save.asp教程";
varpoststr=urlencoding("user_name="+username+"&user_age="+userage+"&user_sex="+usersex);//post时采用编码传递
varajax=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>
<formname="user_info">
姓名:<textareaname="user_name"/></textarea><br/>
年龄:<inputtype="text"name="user_age"/><br/>
性别:<inputtype="text"name="user_sex"/><br/>
<inputtype="button"value="提交表单"onclick="saveuserinfo()">
</form>
<divid="msg"></div>
成功
相关文章
- ajax跨域问题以及解决方案_js跨域请求的三种方法
- ajax实现跨域_js跨域请求的三种方法
- js判断是否包含指定字符串_js分割字符串的方法
- JS算法探险之字符串
- JS字符串补全方法padStart()和padEnd()简介
- js数组对象转字符串方法_数组表示
- js中四舍五入的方法_JS取整
- 在线客服系统复制聊天链接,JS实现复制文本函数
- js关于for循环中的闭包问题详解编程语言
- JS实现php的伪分页
- asp.net下利用js实现返回上一页的实现方法小集
- JS面向对象之神奇的prototype
- js实时监听文本框状态的方法
- js正则表达式之RegExp对象之compile方法编译正则表达式
- js正则表达式之$1$2$3$4$5$6$7$8$9属性,返回子匹配的结果
- Js获取asp页面返回的值(加载值)实现代码
- js关闭当前页面(窗口)的几种方式总结
- js二级地域选择的实现方法
- js每隔5分钟执行一次ajax请求的实现方法
- 禁止IE用右键的JS代码
- 网站繁简切换的JS遇到页面卡死的解决方法
- 原生js获取宽高与jquery获取宽高的方法关系对比
- 三种动态加载js的jquery实例代码另附去除js方法
- Ajax局部更新导致JS事件重复触发问题的解决方法
- js读取cookie方法总结
- node.js中的dns.getServers方法使用说明
- node.js中的querystring.escape方法使用说明
- Web表单提交之disabled问题js解决方法
- 在(ASP/PHP/JSP/html/js)中禁止ajax缓存的方法集锦