【问题标题】:JS script calls a PHP script but then I can't handle the JSON for the htmlJS 脚本调用 PHP 脚本,但我无法处理 html 的 JSON
【发布时间】:2015-07-31 11:01:12
【问题描述】:

我是一个初学者,我正在编写一个 js 函数,它每 1 秒调用一次 PHP 脚本,在该脚本中检查 mysql DB 上的一些值,PHP 将它们编码为 Json,然后 JS 再次对其进行解码并将它们放入 html .

我有两个问题要问你:

-为什么你认为这段代码不能正常工作?它只将第一个值写入<p> 列表。

-我怎样才能使它可扩展?对 php 脚本的每次调用都会执行一个查询。我需要类似实时的东西,但我认为使用这种方式我很快就会破坏我的服务器x​​D

JS

setInterval(function(){

    $.getJSON("php/checkstatus.php", function(result){

                $("#status").html(result['status']);
                $("#tempint").html(result['tempint']);
                $("#tempext").html(result['tempext'] + " ");
                $("#humidityext").html(result['humidityext'] + " ");


        });

  },1000);

json是这样的:

{"status":"0","tempext":"25","tempint":"150","humidityext":"65"}

HTML 是这样的:

<p>status: <span id='status'>xx</span> .</p>
<p>Inside temperature: <span id='tempint'>xx</span> °C</p>
<p>Outside temperature: <span id='tempext'>xx</span> °C</p>
<p>Outside humidity: <span id='humidityext'>xx</span> </p>

【问题讨论】:

  • result['status']result.status 是两个相似的东西。 @ReaganGallant
  • @Viral。你说得对。键:值
  • 您在 Web 控制台中遇到了什么错误?

标签: javascript php mysql json


【解决方案1】:

可能是,在您的 HTML 中,tempinttempexthumidityext 有多个具有相同 id 的元素。

要调试,打开浏览器的控制台,然后查询所有四个元素,例如,

$("#status")

与其余三个类似,现在您将获得 HTML 标签,将鼠标悬停在该标签上,看看您想要的标签是否将数据放在突出显示上。

如果您看到要突出显示的标签不是您想要的,这意味着您有重复的IDs 元素。你需要解决这个问题。

第二个问题,你可以试试http://socketo.me/demo

【讨论】:

    【解决方案2】:

    调用返回的“结果”变量不是数组而是对象(json 代表 JavaScript Object Notation)。您可以使用“.”访问它们,例如:

    $("#status").html(result.status);
    

    无法帮助您解决第二个问题。

    【讨论】:

    • 好提示 :) 这对未来很有帮助!
    【解决方案3】:

    上面的代码似乎没问题。我在本地服务器上测试过,效果很好。

    确保包含一个 jquery 库,例如:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    

    此外,您是否通过网络服务器对其进行测试?

    如果您使用的是 Google Crome,请检查您的控制台选项卡并报告任何错误。

    【讨论】:

    • 实际上这解决了我的问题,你能解释一下如何吗?我已经包括:&lt;script src="//code.jquery.com/jquery.js"&gt;&lt;/script&gt;
    • 你确定吗?使用 效果也很好。
    【解决方案4】:

    尝试使用 jQuery $.ajax 函数而不是 $.getJSON。如果请求失败,您可以访问错误处理。

    function GetData() {
        $.ajax({
            url: 'php/checkstatus.php',
            dataType: 'json'
        }).done(function(result) {
            $("#status").html(result['status']);
            $("#tempint").html(result['tempint']);
            $("#tempext").html(result['tempext'] + " ");
            $("#humidityext").html(result['humidityext'] + " ");
        }); 
    }
    setInterval(function() { GetData(); }, 1000);
    

    这在 IE、Chrome 和 Firefox 中对我有用。

    我使用以下 JSON 文件进行了测试:

    <?php
    $tempext = rand(20, 100);
    $tempint = rand(100, 200);
    $humidityext = rand(30, 100);
    echo '{"status":"0","tempext":"'.$tempext.'","tempint":"'.$tempint.'","humidityext":"'.$humidityext.'"}';
    

    至于您的服务器的性能,请查看这篇文章,了解一个有趣的选项,该选项描述了 Facebook 如何处理他们的一些流量。 How does facebook, gmail send the real time notification?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      相关资源
      最近更新 更多