1

我在使用 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 文件中看到返回的数据,但是 alert(AJAX.responseText) 我只能看到一个空白的警报窗口。

下面也是我的 php 文件,它从 db 读取并将变量发送到 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);

?>
4

2 回答 2

1

当您在以下命令中设置“true”时,您已将 ajax 请求设置为异步:

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

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

    警报(AJAX.responseText);
    var 结果 = AJAX.responseText;

    评估(结果);

必须插入到这里:

AJAX.onreadystatechange = function() { // 当浏览器有请求信息时..
            if (AJAX.readyState==4 || AJAX.readyState=="complete")
            { // 查看是否设置了完整标志。
              //你的代码//
            回调(AJAX.responseText,AJAX.status);// 将响应传递给我们的处理函数
            } // 结束 Ajax 就绪状态检查。
        }
于 2012-08-24T07:53:15.920 回答
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);
于 2012-08-24T08:10:38.960 回答