javascript - PHP SERVER 属性在 javascript 下的函数中不起作用?

标签 javascript php xml

我使用 xml.php 页面生成 xml,如下所示

<?php
$path = trim(ReadData("data_file_path"));
$speed_data_file = $path . "speed";
$rmp_data_file = $path . "rpm";
$temp_data_file = $path . "temp";
$fuel_data_file = $path . "fuel";

header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<table_info>';
    echo "<speed>";
        echo ReadData($speed_data_file);
    echo "</speed>";

    echo "<rpm>";
        echo ReadData($rmp_data_file);
    echo "</rpm>";

    echo "<temp>";
        echo ReadData($temp_data_file);
    echo "</temp>";

    echo "<fuel>";
        echo ReadData($fuel_data_file);
    echo "</fuel>";
echo '</table_info>';

function ReadData($filepath)
{
    $data = file_get_contents($filepath) or null;
    return $data;
}
?>

在我的 main.php 页面下,我正在 body 标记的 onload 事件下读取上述 xml 内容。

...
function Onload() 
{
    var xml_data_file = "http://" + "localhost" + "/demo/xml.php";
    loadXMLDoc(xml_data_file, function() {updateXMLtoElement()});
}

function loadXMLDoc(url,cfunc)
{
    if (window.XMLHttpRequest) {

        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {

        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=cfunc;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}

function updateXMLtoElement()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {

        z =  xmlhttp.responseXML;

        if(z.getElementsByTagName("speed")[0].childNodes[0].nodeValue != null) {
            document.getElementById("speed_dial").setAttribute("data-value", z.getElementsByTagName("speed")[0].childNodes[0].nodeValue);
        }

        if(z.getElementsByTagName("rpm")[0].childNodes[0].nodeValue != null) {
            document.getElementById("rmp_dial").setAttribute("data-value", z.getElementsByTagName("rpm")[0].childNodes[0].nodeValue);
        }

        if(z.getElementsByTagName("temp")[0].childNodes[0].nodeValue != null) {
            document.getElementById("temp_dial").setAttribute("data-value", z.getElementsByTagName("temp")[0].childNodes[0].nodeValue);
        }

        if(z.getElementsByTagName("fuel")[0].childNodes[0].nodeValue != null) {
            document.getElementById("fuel_dial").setAttribute("data-value", z.getElementsByTagName("fuel")[0].childNodes[0].nodeValue);
        }
    }
}
...

一切工作正常,但是当我在上面的代码中更改 Onload 时,如下所示,它停止工作。

function Onload() 
{    
    var xml_data_file = "http://" + "<?php echo $_SERVER['SERVER_ADDR'].":".$_SERVER['SERVER_PORT'];  ?>" + "/demo/xml.php";
    loadXMLDoc(xml_data_file, function() {updateXMLtoElement()});
}

使用 Chrome 的“检查元素”功能,似乎属性 $_SERVER['SERVER_ADDR'] 和 $_SERVER['SERVER_PORT'] 都响应,并以正确的服务器地址和端口进行响应。那么出了什么问题?

最佳答案

通常 Javascript 文件不由 PHP 处理。有非常充分的理由,但超出了范围。 <?php...字符串在 javascript 中按原样使用,而不是您期望的值。

您想要实现的目标应该这样完成:

function Onload() 
{    
    var xml_data_file = "/demo/xml.php";
    loadXMLDoc(xml_data_file, function() {updateXMLtoElement()});
}

当在 http://localhost/demo/xml.php 打开时,它将使用当前页面的架构、域和端口:“http://localhost ” ,以及在 http://example.com/demo/xml.php 打开时的“http://example.com ” .

关于javascript - PHP SERVER 属性在 javascript 下的函数中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41099022/

相关文章:

javascript - 类型错误 : Cannot read property 'go' of undefined in AngularJS

javascript - 为什么表单没有提交

javascript - Angular : Allow selection from only one group in drop-down list bootstrap-select

php - 允许 artisan 服务中的 cors

sql-server - SQL 处理 XML 性能 : Insert into columns in a table

java - Spring Service Activator - 我们可以对 Java 中的两个不同输入 channel 使用相同的方法和 ref 吗?

javascript - Express.js Passport认证失败自动跳过策略

php - 基于颜色的 PHP 图像搜索引擎

javascript - 显示与将作为 php 接收的输入不同的值

xml - 如何配置 BaseX 以在 VS Code 中使用