2012-08-24 42 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);