alert(3)不输出,提示过程调用错误
代码如下:<script>
//创建ajax对象
var Ajax = function(){
var xmlHttp;
}Ajax.prototype.createXMLHttpRequest = function() {
// IE
if (window.ActiveXObject) {
this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
alert(1);
}
// Mozilla
else if (window.XMLHttpRequest) {
this.xmlHttp = new XMLHttpRequest();
}
}Ajax.prototype.send = function(url){
this.createXMLHttpRequest();
alert(2);
this.xmlHttp.open("post",url,true);
alert(3);
this.xmlHttp.onreadystatechange=handleStateChange; //指定响应的函数
this.xmlHttp.send(null); //发送请求
}
//设置回调函数
Ajax.prototype.handleStateChange = function(resFunction) {
if(this.xmlHttp.readyState == 4) {
if(this.xmlHttp.status == 200) {
resFunction();
}
}
}
</script>
<body>
<script>
var myReq = new Ajax();
myReq.send();
</script>
</body>
代码如下:<script>
//创建ajax对象
var Ajax = function(){
var xmlHttp;
}Ajax.prototype.createXMLHttpRequest = function() {
// IE
if (window.ActiveXObject) {
this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
alert(1);
}
// Mozilla
else if (window.XMLHttpRequest) {
this.xmlHttp = new XMLHttpRequest();
}
}Ajax.prototype.send = function(url){
this.createXMLHttpRequest();
alert(2);
this.xmlHttp.open("post",url,true);
alert(3);
this.xmlHttp.onreadystatechange=handleStateChange; //指定响应的函数
this.xmlHttp.send(null); //发送请求
}
//设置回调函数
Ajax.prototype.handleStateChange = function(resFunction) {
if(this.xmlHttp.readyState == 4) {
if(this.xmlHttp.status == 200) {
resFunction();
}
}
}
</script>
<body>
<script>
var myReq = new Ajax();
myReq.send();
</script>
</body>
myReq.send("这个函数需要指定一个你请求的url");
this.xmlHttp.onreadystatechange=Ajax.prototype.handleStateChange;
在就是this.xmlHttp.open("post",url,true); 中的post应该改为GET吧。
2. send方法要修改一下:Ajax.prototype.send = function(url) {
// save 'this' object
var that = this; this.createXMLHttpRequest();
alert(2);
this.xmlHttp.open("post",url,true);
alert(3); this.xmlHttp.onreadystatechange = function() {
// make use of closure to save current context
that.handleReadyState();
};
this.xmlHttp.send(null); //发送请求
};