javascript

位置:IT落伍者 >> javascript >> 浏览文章

js调用AJAX时Get和post的乱码


发布日期:2022年12月01日
 
js调用AJAX时Get和post的乱码

在使用"get"时抓取的页面最后加上编码类型

代码如下:<%服务器端servletactioncontextgetresponse()setcharacterencoding("utf");客户端 网页特效p/jsphtml target=_blank >jsp教程 <%@ page language="java" contenttype="text/html; charset=utf" pageencoding="utf"%>responseexpires = responseaddheader "pragma""nocache"responseaddheader "cachectrol""nocache"responseaddheader "contenttype""text/html; charset=gb"这是重点否则会出现乱码responsewrite "中文汉字"%>在使用post时用vbscript解决了编码问题源码如下代码如下:<script language="vbscript">function urlencoding(vstrin)strreturn = ""for i = to len(vstrin)thischr = mid(vstrini)if abs(asc(thischr)) < &hff thenstrreturn = strreturn & thischrelseinnercode = asc(thischr)if innercode < theninnercode = innercode + &hend ifhight = (innercode and &hff) &hfflow = innercode and &hffstrreturn = strreturn & "%" & hex(hight) & "%" & hex(low)end ifnexturlencoding = strreturnend functionfunction bytesbstr(vin)strreturn = ""for i = to lenb(vin)thischarcode = ascb(midb(vini))if thischarcode < &h thenstrreturn = strreturn & chr(thischarcode)elsenextcharcode = ascb(midb(vini+))strreturn = strreturn & chr(clng(thischarcode) * &h + cint(nextcharcode))i = i + end ifnextbytesbstr = strreturnend function</script>下面是使用vbscript函数代码如下:<script language=网页特效>/*** 初始化一个xmlhttp对象*/function initajax(){var ajax=false;try {ajax = new activexobject("msxmlxmlhttp");} catch (e) {try {ajax = new activexobject("microsoftxmlhttp");} catch (e) {ajax = false;}}if (!ajax && typeof xmlhttprequest!=undefined) {ajax = new xmlhttprequest();}return ajax;}function saveuserinfo(){var msg = documentgetelementbyid("msg");var f = documentuser_info;var username = fuser_namevalue;var userage = fuser_agevalue;var usersex = fuser_sexvalue;var url = "saveasp教程";var poststr = urlencoding("user_name="+ username +"&user_age="+ userage +"&user_sex="+ usersex);//post时采用编码传递var ajax = initajax();ajaxopen("post" url true);ajaxsetrequestheader("contenttype""application/xwwwformurlencoded");ajaxsend(poststr);ajaxonreadystatechange = function() {if (ajaxreadystate == && ajaxstatus == ) {msginnerhtml = bytesbstr(ajaxresponsebody); //获取时解码}}}</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>成功

上一篇:jquery图片不完全按比例自动缩小的简单代码

下一篇:跟我一起学JQuery插件开发