52,797
社区成员
发帖
与我相关
我的任务
分享
xhr.open("get","xxxx.aspx?_dc="+new Date().getTime(),true);
if(xhr.status==200||xhr.status==0){
alert('ok');
}
<?xml version="1.0" encoding="utf-8"?>
<showbo>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</showbo>
function getajax(){
if(window.XMLHttpRequest)return new XMLHttpRequest();
else if(window.ActiveXObject)return new ActiveXObject("microsoft.xmlhttp");
}
var xhr=getajax();
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200||xhr.status==0){
var doc=xhr.responseXML,item=doc.getElementsByTagName("item");
alert(item.length);//在ie输出为0,在ff下为4。似乎在ie下未生成xml的树结构,具体原因要问ms了。。
}
else alert('发生错误\n\n'+xhr.status);
}
}
xhr.open("get","showbo.xml?_dc="+new Date().getTime(),true);
xhr.send(null);
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200||xhr.status==0){
var doc=xhr.responseXML;
if(document.all&&xhr.status==0){//为ie并且直接托进浏览器的时重构xml的树结构
doc=new ActiveXObject("microsoft.xmldom");
doc.loadXML(xhr.responseText);
doc=doc.documentElement;
}
var item=doc.getElementsByTagName("item");
alert(item.length);//在ie输出为0,在ff下为4。似乎在ie下未生成xml的树结构,具体原因要问ms了。。
}
else alert('发生错误\n\n'+xhr.status);
}
}
xhr.open("post","xxxx.aspx",true);
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");//这里。。。。
Session.CodePage=65001
Response.CharSet="utf-8"
因为asp在国内服务器默认处理编码为gb2312<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
,并且在输出中文前设置Response.CharSet="gb2312";function callServerByPost(url,data,fun) {
var http_request=null;
if (window.ActiveXObject)http_request = new ActiveXObject("Microsoft.XMLHTTP");
}else if (window.XMLHttpRequest) http_request = new XMLHttpRequest();
if (!http_request) {
alert('Giving up :Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = fun;
http_request.open("POST", url, true);
http_request.setrequestheader("Content-length",data.length);
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
http_request.send(data);//传送资料
}
function ajax_post(url,data) {
url=url+"?t="+new Date();
callServerByPost(url,data,function fns(){
if(http_request.readyState == 4) {
if (http_request.status == 200) {
return http_request.responseText;//在这里明明调试时http_request.responseText已经有值但是在外边却接收不到
} else {
alert("你请求数据有错");
}
}
});
}
function getData(){
var url="ajax_server.aspx";
var data="name=ljp&pwd=ljp";
var t=ajax_post(url,data);
alert(t);//在这里弹出undefined =============================
}