javascript中的responseText有困难

时间:2012-08-24 07:40:30

标签: javascript ajax json

我有一个问题,使用json将变量从php传递到javascript基本上问题是在我的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);

?>

2 个答案:

答案 0 :(得分:1)

当您设置&#39; true&#39;时,您已将ajax请求设置为异步。在以下命令中:

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

这意味着只有当AJAX.readyState == 4时,ajax响应才会准备就绪。 因此,您必须输入以下代码

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

答案 1 :(得分: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);