异步通信函数关于ajax的一点疑问

首先声明,我是一个菜鸟。一下文章中出现技术误导情况盖不负责

    今天在调试程序的时候现发了一个奇异的题问,我写了一个封装ajax的对象,名字就叫ajax:

 1 var ajax = {
 2     get : function(url, onOk) {
 3         var xmlHttpRequest = this.createXMLHttpRequest();
 4         xmlHttpRequest.open("GET", url, true);
 5         xmlHttpRequest.onreadystatechange = function() {
 6             if (this.readyState == 4) {
 7                 if (this.status == 200) {
 8                     onOk(responseText);
 9                 } else {
10                     onOk("异步通信失败");
11                 }
12             }
13         };
14         xmlHttpRequest.send(null);
15     },
16     post : function(url, param, onOk) {
17         var xmlHttpRequest = this.createXMLHttpRequest();
18         xmlHttpRequest.open("POST", url, true);
19         xmlHttpRequest.onreadystatechange = function() {
20             if (this.readyState == 4) {
21                 if (this.status == 200) {
22                     onOk(responseText);
23                 } else {
24                     onOk("异步通信失败");
25                 }
26             }
27         };
28         xmlHttpRequest.send(param);
29     },
30     createXMLHttpRequest : function() {
31         if (window.XMLHttpRequest) {
32             return new XMLHttpRequest();
33         } else if (window.ActiveXObject) {
34             return new ActiveXObject("Microsoft.XMLHTTP");
35         }
36     }
37 };
但是一直没法用调到onOk这个函数,即使服务器端返回了确正的结果,一直用调不到onOk这个函数,后来现发。。。
    每日一道理
父亲对于儿子来说,是座耸立的高山,而儿子只是颗石子,源于山,却并不了解山。生活中诸多爱的密码,是需用细节来解读的,在亲情的沃土上,要想搞得最美的果实,惟有期待那存在于瞬间的心与心的共鸣,爱与爱的默契。
 1 var ajax = {
 2     get : function(url, onOk) {
 3         var xmlHttpRequest = this.createXMLHttpRequest();
 4         xmlHttpRequest.open("GET", url, true);
 5         xmlHttpRequest.onreadystatechange = function() {
 6             if (this.readyState == 4) {
 7                 if (this.status == 200) {
 8                     onOk(this.responseText);
 9                 } else {
10                     onOk("异步通信失败");
11                 }
12             }
13         };
14         xmlHttpRequest.send(null);
15     },
16     post : function(url, param, onOk) {
17         var xmlHttpRequest = this.createXMLHttpRequest();
18         xmlHttpRequest.open("POST", url, true);
19         xmlHttpRequest.onreadystatechange = function() {
20             if (this.readyState == 4) {
21                 if (this.status == 200) {
22                     onOk(this.responseText);
23                 } else {
24                     onOk("异步通信失败");
25                 }
26             }
27         };
28         xmlHttpRequest.send(param);
29     },
30     createXMLHttpRequest : function() {
31         if (window.XMLHttpRequest) {
32             return new XMLHttpRequest();
33         } else if (window.ActiveXObject) {
34             return new ActiveXObject("Microsoft.XMLHTTP");
35         }
36     }
37 };
加上this关键字以可就用调到了,我很疑惑,为什么不添加this关键字,为什么就用调不到onOk这个函数呢?

    来原在onOk在所函数和外部都没有这个responseText变量,只有指定了this,才能精确位定到XMLHTTPRequest对象中的属性

文章结束给大家分享下程序员的一些笑话语录: 这个世界上只有10种人:懂得二进制的和不懂得二进制的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值