當試圖從簡單的香草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;
}
}
}
}
}
而且,這裏是從螢火蟲認爲
如何去響應參考從ajax調用?
看一看:https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started –