簡體   English   中英

從Ajax呼叫獲得響應

[英]Getting response From Ajax call

當嘗試從使用普通香草javascript構建的ajax調用中獲取responseText時,Firebug似乎看到了請求,但無法獲得對responseText的引用。

這是功能代碼

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;
            }
            }
        }
    }
}

這也是螢火蟲的景色 在此處輸入圖片說明

如何獲得對ajax調用響應的引用?

OnReadyStateChanged需要為onreadystatechange JavaScript區分大小寫。

ajaxObj.onReadyStateChangedonreadystatechange應該全部為小寫(並且不帶尾隨的“ d”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM