2013-07-29 104 views
0

当试图从简单的香草JavaScript中构建的ajax调用中获取responseText时,Firebug似乎看到请求,但无法获取对responseText的引用。从Ajax调用获取响应

这是函数的代码

function getAjaxResponse(){  
    var ajaxObj = getAjaxObj(); 
    ajaxObj.open('get', 'responsePage.php', true); 
    ajaxObj.onReadyStateChanged = function(){ 
     if(ajaxObj.readyState == 4 
      && ajaxObj.status == 200){ 
       //no functions are getting fired in here     
       //this does not get logged to console 
       console.log(ajaxObj.responseText); 
       //neither does this 
       console.log(2); 
     } 
    }; 
    ajaxObj.send(null); 

    //this does gets logged to console 
    console.log(1); 
} 

功能Ajax对象

function getAjaxObj(){ 
    var req; 
    if(window.XMLHttpRequest){ 
     try{ 
      req = new XMLHttpRequest();                 
     } catch(e){ 
      req = false; 
     } finally { 
      return req; 
     } 
    } else { 
     if(window.ActiveXObject){ 
      try{ 
       req = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch(e){ 
       try{ 
        req = new ActiveXObject("Msxml.XMLHTTP"); 
       } catch(e){ 
        req = false; 
       } finally { 
        return req; 
      } 
      } 
     } 
    } 
} 

而且,这里是从萤火虫认为 enter image description here

如何去响应参考从ajax调用?

+0

看一看:https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started –

回答

2

OnReadyStateChanged需要是onreadystatechange。 JavaScript区分大小写。

+0

哈哈..非常感谢,越来越好p!#$#d。只要它能让我接受你的答案!欢呼 – user866190

+0

@ user866190乐于帮忙! – marteljn

1

ajaxObj.onReadyStateChangedonreadystatechange应该都是小写(并且没有结尾的 'd')

+0

有没有... – marteljn

+0

错别字对不起 - 更正 – Alfie