2011-02-11 49 views

回答

2

該引擎收錄代碼已經這樣做了。我猜你實際面臨的問題是因爲你的ajax調用是異步,這意味着你正在發出ajax請求(異步),並立即嘗試訪問全局變量中的值 - 但它尚未設定。

解決方法是在onReadyStateChange回調中執行您的post-ajax代碼。

function handleResponse(result_cont) { 
    // your result_cont processing code here 
} 

ajax(handleResponse); 

function ajax(callback) { 
    var xmlHttp; 
    try { // Firefox, Opera 8.0+, Safari 
     xmlHttp = new XMLHttpRequest(); 
    } catch (e) { 
     // Internet Explorer 
     try { 
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try { 
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e) { 
       return false; 
      } 
     } 
    } 
    xmlHttp.onreadystatechange = function() { 
     if (xmlHttp.readyState == 4) { 
      if (xmlHttp.responseText != "") { 
       result_cont = xmlHttp.responseText 
       alert(result_cont); 

       // ############# here's the important change ############# 
       // execute the provided callback 
       callback(result_cont); 
      } 
     } 
    } 
    xmlHttp.open("GET", "contentdetails.php?cid=1", true); 
    xmlHttp.send(null); 
}