用js来解决ajax读取页面乱码

时间:2024-04-18 10:56:04 

例子:
rsajax.js:


var http_request = false;
function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//http_request.setRequestHeader("Content-Type","application/text/html; charset:utf-8");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
if(!Sys.ie){
http_request.overrideMimeType("text/html;charset=gb2312");
}
http_request.send(parameters);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
if(Sys.ie){
result = http_request.responseBody;
}
else
{
result = http_request.responseText;
}
//newresult=result.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"&#x$2;")});
//newreslt=unescape(result.replace(/&#x/g,'%u').replace(/;/g,'')); //utf-8和汉字互转,不需要了
document.getElementById('waiting').innerHTML = '';
if(Sys.ie){
document.getElementById('showresults').innerHTML =gb2utf8(result); }
else{
document.getElementById('showresults').innerHTML =result;
}
} else {
alert('There was a problem with the request.');
}
}else{
document.getElementById('waiting').innerHTML = '<img src="loading.gif">';}
}
function get(obj) {
//var poststr = "accounts=" + encodeURI( document.getElementById("accounts").value );
makePOSTRequest('y.htm', '');
}
//ie引入一个杂交函数,nndx
function gb2utf8(data){
var glbEncode = [];
gb2utf8_data = data;
execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");
t=t.split("@");
var i=0,j=t.length,k;
while(++i<j) {
k=t[i].substring(0,4);
if(!glbEncode[k]) {
gb2utf8_char = eval("0x"+k);
execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript");
glbEncode[k]=escape(gb2utf8_char).substring(1,6);
}
t[i]=glbEncode[k]+t[i].substring(4);
}
gb2utf8_data = gb2utf8_char = null;
return unescape(t.join("%"));
}
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
if (window.ActiveXObject)
Sys.ie = ua.match(/msie ([\d.]+)/)[1]
else if (document.getBoxObjectFor)
Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
else if (window.MessageEvent && !document.getBoxObjectFor)
Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1]
else if (window.opera)
Sys.opera = ua.match(/opera.([\d.]+)/)[1]
else if (window.openDatabase)
Sys.safari = ua.match(/version\/([\d.]+)/)[1];


index.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title></title>
<script type="text/javascript" language="javascript" src="rsajax.js"></script>
<HEAD>
</head>
<body>
<div align="center">
<input onclick="javascript:get(document.getElementById('acctform'));" name="acctform" id="acctform" type=button>
<div align="center">
<div id="waiting"></div>
<p>
<div id="showresults"></div>
</div>
<BR>
<body></html>


y.htm
我们大家一起来,嘿嘿

标签:ajax,页面乱码
0
投稿

猜你喜欢

  • 使用pt-kill根据一定的规则来kill连接的方法

    2024-01-26 01:18:37
  • 基于Python中求和函数sum的用法详解

    2022-12-05 23:26:18
  • 怎么样用xmlhttp读取远程xml的数据

    2008-10-11 13:52:00
  • 使用Python的datetime库处理时间(RPA流程)

    2023-04-23 22:53:15
  • Python实现CNN的多通道输入实例

    2021-10-10 11:33:07
  • Java正则表达式基础语法详解

    2023-07-10 14:42:14
  • Oracle密码错误次数过多账号锁定的问题(推荐)

    2024-01-14 13:24:05
  • python操作kafka实践的示例代码

    2021-04-19 10:57:34
  • Python+Pygame实现简单的射击小游戏

    2023-06-01 11:50:30
  • python连接字符串的方法小结

    2023-12-29 16:24:49
  • 用 JavaScript 解数学题

    2010-07-09 13:38:00
  • Numpy数组array和矩阵matrix转换方法

    2021-06-25 06:17:26
  • layui 富文本图片上传接口与普通按钮 文件上传接口的例子

    2024-05-22 10:36:28
  • vue开发心得和技巧分享

    2024-04-30 10:30:27
  • sql 语句插入结果为select和值混合示例

    2024-01-29 09:12:42
  • wxPython事件驱动实例详解

    2021-09-21 18:25:11
  • javascript中substring()、substr()、slice()的区别

    2024-04-16 09:54:35
  • MySQL基础教程之IN的用法详解

    2024-01-16 19:36:32
  • Node.js对MongoDB数据库实现模糊查询的方法

    2024-01-23 14:31:02
  • 理清mysql外链的含义和命令

    2010-03-09 15:55:00
  • asp之家 网络编程 m.aspxhome.com