js调用AJAX时Get和post的乱码解决方法
发布时间:2024-05-09 10:38:11
标签:AJAX,Get,post,乱码
在使用"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>
成功


猜你喜欢
- 这篇文章主要介绍了Python接口自动化判断元素原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 在还源数据库时出现以下错误(详情参见下图):System.Data.SqlClient.SqlError: 在对 'C:\Progr
- CSS命名规范一.文件命名规范全局样式:global.css;框架布局:layout.css;字体样式:font.css;链接样式:link
- 早上一开Google Reader,发现已经全部被Silverlight 3的消息占领了,基本上都来自于Silverlight
- 本篇阅读的代码片段来自于30-seconds-of-python。1、average_bydef average_by(lst, fn=la
- 一 CGI.pm中的方法(routines)调用1. CGI.pm实现了两种使用方法,分别是面向对象的方式和传统的perlmodule方法的
- 本文实例讲述了python基于xml parse实现解析cdatasection数据的方法,分享给大家供大家参考。具体实现方法如下:from
- 获取输入的五个点画五边形def pentagonUpdate(): p = {} win = GraphWin(&q
- 本文实例为大家分享了Vue自定义日历插件的具体代码,供大家参考,具体内容如下由于网上的插件没有符合项目的需求决定自己实现图示如下:默认选择今
- import randomdef genrand(small, big) : return small
- 1. 简介本文介绍使用sync.Once来实现单例模式,包括单例模式的定义,以及使用sync.Once实现单例模式的示例,同时也比较了其他单
- 在Python的学习过程中,肯定会遇到很多安装模块的地方,可以使用easy_install安装,但是easy_install相对于pip而言
- 今天使用os.path.isdir()判断是否是文件夹的时候发现一个问题:lst = os.listdir(path) &nb
- JavaScript简介一、定义:JavaScript是脚本语言,需要宿主文件,它的宿主文件是html文件。JavaScript 是一种轻量
- MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函
- vscode来写python,配置灵活,界面美观,是个非常好的选择。我这里是在ubuntu系统下配置vscode的python3开发环境,当
- 一、前言构建命令行程序很酷:命令行可以按照我们的设定完成相应的工作,相比 GUI 界面程序,无需花费大量时间设计 GUI 界面。但要使命令行
- 下面说说主要实现思路: 1、存取图片 (1)、将图片文件转换为二进制并直接存进sql server //UploadHelper.cs //
- 在前一篇博客我们学习了 Go 数组,其要求所有元素为同一数据类型,如果希望存储不同类型的数据,就要用到结构体相关知识。结构体的定义:存储相同
- 下面方法可以用来快速生成一批数据 if(object_id('t') is not null) drop table t g