【发布时间】:2014-09-02 21:56:51
【问题描述】:
我已经定义了一个类,我正在尝试使用XMLHttpRequest 获取一个 HTML 文件并将响应分配给类中的变量,但它不会改变。
function UIHandler(){
this.notificatoin = 0;
this.msgBoxMe = 1;
this.msgBoxThem = 2;
this.getMsgBox(1);
this.getMsgBox(2);
}
UIHandler.prototype.getMsgBox = function(me){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){//here we have the template in xhr.responseText
this.msgBoxMe = xhr.responseText;
}
};
switch(me){
case 1:
xhr.open("GET" , "chat/views/me.html" , true);
break;
case 2:
xhr.open("GET" , "chat/views/them.html" , true);
break;
}
xhr.send();
};
我将onreadystatechange 事件处理程序中的响应分配给this.msgBoxMe 变量,但它的值仍然是1。
【问题讨论】:
-
你意识到它是异步的吗?
-
旁注:JavaScript 没有类,请参阅Prototype-based programming。
标签: javascript jquery ajax