用js来解决ajax读取页面乱码
例子:
rsajax.js:
index.php
y.htm
我们大家一起来,嘿嘿
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,"$2;")}); //newreslt=unescape(result.replace(//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
我们大家一起来,嘿嘿
相关推荐
lyqdanang 2020-08-16
坚持着执着 2020-06-16
坚持着执着 2020-07-16
坚持着执着 2020-06-14
TONIYH 2020-06-05
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
jiaguoquan00 2020-07-07