2012-08-24 40 views
1

我在使用json傳遞變量從php到JavaScript的問題基本上問題是,在我的javascript我可以調試和查看responseText中的項目,但我不能將它們分配給變量或查看他們,我也試過一個單一的項目,但沒有管理爲什麼發生這種情況的任何想法。與JavaScript中的responseText有困難

function ajaxrequestDB() { 
    var AJAX = null; // Initialize the AJAX variable. 

    if (window.XMLHttpRequest) { // Does this browser have an XMLHttpRequest object? 
     AJAX=new XMLHttpRequest(); // Yes -- initialize it. 
    } 
    else { // No, try to initialize it IE style 
     AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again? 
    } // End setup Ajax. 

    if (AJAX==null){ // If we couldn't initialize Ajax... 
    alert("Your browser doesn't support AJAX."); // Sorry msg. 
    return false // Return false, couldn't set up ajax 
    } 
     AJAX.onreadystatechange = function() { // When the browser has the request info.. 
      if (AJAX.readyState==4 || AJAX.readyState=="complete") 
      { // see if the complete flag is set. 
      callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function 
      } // End Ajax readystate check. 
     } 

    var url='http://localhost/Scripts/refresh.php'; 
    //var url='http://cpdtest.zzl.org/Scripts/hidemarker.php?Name='+myname; 
    AJAX.open("GET", url, true); // Open the url this object was set-up with. 
    AJAX.send(); // Send the request.  
    alert(AJAX.responseText); 
    var result = AJAX.responseText; 

    eval(result); 
    //alert(result); 
} 

從上面的,如果我做了調試上AJAX.responseText我可以從我的PHP文件中看到返回的數據,但警報(AJAX.responseText),我只能看到一個空白的警告窗口。

下面也是我的PHP文件,它從數據庫讀取並將變量發送到JavaScript。

<?php 
header('Content-type: application/json'); 
//$con = mysql_connect("localhost","770132_admin","admin"); 
$con = mysql_connect("localhost","root",""); 

if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("cpd", $con); 

$SQL = "SELECT name from markers"; 
$result = mysql_query($SQL); 
while ($db_field = mysql_fetch_assoc($result)){ 

$data[]= $db_field; 

} 
echo json_encode($data); 

?> 

回答

1

您已設置Ajax請求,當你設置的「真」在下面的命令是異步的:

 
AJAX.open("GET", url, true); 

這意味着Ajax響應將準備只有當AJAX.readyState == 4。 因此,你必須把下面的代碼

 
    alert(AJAX.responseText); 
    var result = AJAX.responseText; 

    eval(result); 

必須插入到這裏:

 
AJAX.onreadystatechange = function() { // When the browser has the request info.. 
      if (AJAX.readyState==4 || AJAX.readyState=="complete") 
      { // see if the complete flag is set. 
       //YOUR CODE// 
      callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function 
      } // End Ajax readystate check. 
     } 
+0

是的它現在的工作,但我」我遇到的還有,我想訪問對象的屬性。我從php傳遞一個字符串,如[{「name」:「Salini」},{「name」:「TAllauOmmu」},{「name」:「Test William」}]其中name是顏色名稱從我從中提取數據的數據庫。所以當調用alert(result)時它正在工作,因爲它正在返回整個字符串,但我希望我可以得到Salini,TAllauOmmu和TEST William。如果我做alert(result.name)我得到undefined。你有什麼想法可以解決問題嗎? – Etienne

+0

你應該首先[解碼json](http://stackoverflow.com/a/4174137/797303)。 –

+0

說實話我可以使用JSON.parse,但即使如此我仍然得到[對象對象]作爲返回值,如果我做一個alert(result.name),那麼我得到undefined ...任何想法?這是螢火\t 結果 \t [對象名{= 「TAllauOmmu」}]輸出 \t 對象{名稱= 「TAllauOmmu」} \t名 「TAllauOmmu」 – Etienne

0

嘗試使用下一個javascript代碼:

function ajaxrequestDB(callback) { 
    var AJAX; // Initialize the AJAX variable. 
    if (window.XMLHttpRequest) { // Does this browser have an XMLHttpRequest object? 
     AJAX=new XMLHttpRequest(); // Yes -- initialize it. 
    } 
    else if (window.ActiveXObject) { // No, try to initialize it IE style 
     AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again? 
    } // End setup Ajax. 

    if (!AJAX) { // If we couldn't initialize Ajax... 
     alert("Your browser doesn't support AJAX."); // Sorry msg. 
     return false; // Return false, couldn't set up ajax 
    } 

    AJAX.onreadystatechange = function() { // When the browser has the request info.. 
     if (AJAX.readyState==4) { // see if the complete flag is set. 
      callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function 
     } // End Ajax readystate check. 
    } 
    var url='http://localhost/Scripts/refresh.php'; 
    //var url='http://cpdtest.zzl.org/Scripts/hidemarker.php?Name='+"myname"; 
    AJAX.open("GET", url, true); // Open the url this object was set-up with. 
    AJAX.send(null); // Send the request.  
} 

function showResult(text, status){ 
    if(status==200){ 
     alert(text); 
     var result=JSON.parse(text); 
     alert(result); 
    } 
    else alert("HTTP Error:" + status); 
} 

ajaxrequestDB(showResult);