2013-07-29 134 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