52,797
社区成员
发帖
与我相关
我的任务
分享
<!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>Ajax 封装</title>
<style type="text/css">
</style>
<script language="javascript">
function Ajax(surl,smenthod,sparas,basyn,fun){
this.url = surl;
this.menthod = smenthod;
this.paramts = sparas;
this.asynchronism = basyn;
if(typeof(fun) == "function"){
this.callfun = fun;
}else{
}
this.ajax =this.creatAjax();
}
Ajax.prototype.creatAjax = function(){
var xmlHttp=false;
if(!xmlHttp && typeof(XMLHttpRequest)!="undefined")
{
xmlHttp=new XMLHttpRequest();
}
return xmlHttp;
}
Ajax.prototype.callServer = function(){
var str='';
if(this.paramts){
var temp1 = this.paramts.split(",");
var len = temp1.length;
var temp2;
for(var i=0;i<len;i++){
temp2 = temp1[i].split(":");
if(i != len-1){
str += temp2[0]+"="+temp2[1]+"&";
}else{
str += temp2[0]+"="+temp2[1];
}
}
}
if(str.length > 0){
if(this.menthod == "GET"){
this.url = this.url+"?"+str;
}
}
this.ajax.open(this.menthod,this.url,this.asynchronism);
this.ajax.setRequestHeader("Content-Type","text/xml");
this.ajax.onreadystatechange = this.backFun;
if(this.menthod == "GET"){
this.ajax.send(null);
}else{
if(str.length >0 ){
this.ajax.send(str);
}
}
}
Ajax.prototype.backFun = function(){
if( this.readyState == 4 || this.readyState == 200){
var data = this.responseText;
this.callfun(sdata); //这行出错,this指向有问题
}
}
window.onload = function(){
var ajax1 = new Ajax("ajax.html","GET","",true,function(data){
document.getElementById("p1").innerHTML = data;
});
ajax1.callServer();
}
</script>
</head>
<body>
<p id="p1"></p>
</body>
</html>
Ajax.prototype.creatAjax = function(){
var xmlHttp=false;
if( typeof(XMLHttpRequest)!="undefined")
{
xmlHttp=new XMLHttpRequest();
}else if(window.ActiveXObject)xmlHttp=new ActiveXObject("msxml2.xmlhttp.5.0");
if(!xmlHttp)throw new Exception("浏览器不支持Ajax!");
return xmlHttp;
}
Ajax.prototype.callServer = function(){
//....其他代码
this.ajax.onreadystatechange =function(me){//这样来添加状态转换函数,那么backFun中的this对象就指向Ajax的实例对象了,注意修改backFun对象中代码
return function(){
me.backFun();
}
}(this);
if(this.menthod == "GET"){
this.ajax.send(null);
}else{
//if(str.length >0 ){//加这个判断干吗?如果为post没传递参数就不发送请求了
this.ajax.send(str);
//}
}
}
Ajax.prototype.backFun = function(){
//if( this.readyState == 4 || this.readyState == 200){//是status属性吧?怎么两个readyState,并且status要readyState==4时才能访问,要不出错
if( this.ajax.readyState == 4){//注意此时this对象为Ajax的实例对象,而不是指向XMLHttpRequest这个对象
var data = this.ajax.responseText;
this.callfun(sdata);
}
}
Ajax.prototype.backFun = function(){
if( this.readyState == 4 || this.readyState == 200){
var data = this.responseText;
//this.callfun(sdata); //这行出错,this指向有问题
//应该不是指向问题
this.callfun = fun() {};不是传递一个function么,怎么当方法使了
//or this.callfun = f; //f是一个方法名
}
}
Ajax.prototype.backFun = (function(a){
if( this.readyState == 4 || this.readyState == 200){
var data = this.responseText;
a.callfun(sdata); //这行出错,this指向有问题
}
})(Ajax);//这里传递你的实际ajax对象, var ajax1 = new Ajax
window.onload = function(){
var ajax1 = new Ajax("ajax.html","GET","",true,function(data){
document.getElementById("p1").innerHTML = data;
});
ajax1.callServer();
}