javascript - Google Apps 脚本 Web 应用程序 AJAX、jQuery、JSON?

标签 javascript jquery ajax json google-apps-script

我正在尝试遵循“https://learn.sparkfun.com/tutorials/electric-imp-breakout-hookup-guide/all#example-3-web-response”上的“示例 3:Web 响应”

我在 script.google.com 上实现了代码,但无法看到引脚读数。有人可以帮我吗!这是代码

http://jsfiddle.net/8GdLw/44/

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
    $( function() {   
        // Edit these values first! The externalURL variable should be the
        // unique URL of your agent. e.g. the last part of:
        // https://agent.electricimp.com/UpyYpRLmBB7m
        // pollRate defines how often the values on your page will refresh.
        var externalURL ="8XpIqEEdiILG";
        var pollRate ="1000";

        function poll(){
            // Construct an ajax() GET request.
            // http://www.w3schools.com/jquery/ajax_ajax.asp

            $.ajax({
                type: "get",
                url: "https://agent.electricimp.com/"+externalURL,  // URL of our imp agent.
                dataType: "json",   // Expect JSON-formatted response from agent.
                success: function(agentMsg) {   // Function to run when request succeeds.

                    // jQuery find "pin1" id and overwrite its data with "pin1" key value in agentMsg
                    $("#pin1").html(agentMsg.pin1);             
                    $("#pin2").html(agentMsg.pin2);
                    $("#pin5").html(agentMsg.pin5);
                    $("#pin7").html(agentMsg.pin7);
                    $("#pin8").html(agentMsg.pin8);
                    $("#pin9").html(agentMsg.pin9);
                    $("#vin").html(agentMsg.voltage);

                    updateBG(agentMsg.pin5);    // Try this if you have a photocell connected to pin 5
                },
                error: function(err) {
                    console.log("err"+ err.status)
                }
            });
        }

        // setInterval is Javascript method to call a function at a specified interval.
        // http://www.w3schools.com/jsref/met_win_setinterval.asp
        setInterval(function(){ poll(); }, pollRate);

        // This function updates the 
        function updateBG(lightSensor)
        {
            if (lightSensor > 30000)
            {
                document.body.style.backgroundColor = "#FFFFFF";
            }
            else
            {
                document.body.style.backgroundColor = "#AAAAAA";
            }
        }
    });
</script>

        <h3>Imp Pins:</h3>
    <div id="pins">
    <p> <b>Pin 1:</b> <span id="pin1"><!-- This is where the pin 1 reading will go --></span></p>
    <p> <b>Pin 2:</b> <span id="pin2"><!-- This is where the pin 2 reading will go --></span></p>
    <p> <b>Pin 5:</b> <span id="pin5"><!-- This is where the pin 5 reading will go --></span></p>
    <p> <b>Pin 7:</b> <span id="pin7"><!-- This is where the pin 7 reading will go --></span></p>
    <p> <b>Pin 8:</b> <span id="pin8"><!-- This is where the pin 8 reading will go --></span></p>
    <p> <b>Pin 9:</b> <span id="pin9"><!-- This is where the pin 9 reading will go --></span></p>
    <p> <b>Voltage:</b> <span id="vin"><!-- This is where the voltage reading will go --></span></p>

适用于 jfiddle

我非常感谢这里的帮助。

谢谢

最佳答案

这是因为当元素不存在时脚本先执行。

脚本标签应该在声明具有 pin ID 的标签之后。

类似于(在内部)

        <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>
        $( function() {   
        // Edit these values first! The externalURL variable should be the
        // unique URL of your agent. e.g. the last part of:
        // https://agent.electricimp.com/UpyYpRLmBB7m
        // pollRate defines how often the values on your page will refresh.
        var externalURL ="8XpIqEEdiILG";
        var pollRate ="1000";

        function poll(){
            // Construct an ajax() GET request.
            // http://www.w3schools.com/jquery/ajax_ajax.asp

            $.ajax({
            type: "get",
            url: "https://agent.electricimp.com/"+externalURL,  // URL of our imp agent.
            dataType: "json",   // Expect JSON-formatted response from agent.
            success: function(agentMsg) {   // Function to run when request succeeds.

                // jQuery find "pin1" id and overwrite its data with "pin1" key value in agentMsg
                $("#pin1").html(agentMsg.pin1);             
                $("#pin2").html(agentMsg.pin2);
                $("#pin5").html(agentMsg.pin5);
                $("#pin7").html(agentMsg.pin7);
                $("#pin8").html(agentMsg.pin8);
                $("#pin9").html(agentMsg.pin9);
                $("#vin").html(agentMsg.voltage);

                updateBG(agentMsg.pin5);    // Try this if you have a photocell connected to pin 5
            },
            error: function(err) {
                console.log("err"+ err.status)
            }
            });
        }

        // setInterval is Javascript method to call a function at a specified interval.
        // http://www.w3schools.com/jsref/met_win_setinterval.asp
        setInterval(function(){ poll(); }, pollRate);

        // This function updates the 
        function updateBG(lightSensor)
        {
            if (lightSensor > 30000)
            {
            document.body.style.backgroundColor = "#FFFFFF";
            }
            else
            {
            document.body.style.backgroundColor = "#AAAAAA";
            }
        }
        });
    </script>

 <h3>Imp Pins:</h3>
<div id="pins">
<p> <b>Pin 1:</b> <span id="pin1"><!-- This is where the pin 1 reading will go --></span></p>
<p> <b>Pin 2:</b> <span id="pin2"><!-- This is where the pin 2 reading will go --></span></p>
<p> <b>Pin 5:</b> <span id="pin5"><!-- This is where the pin 5 reading will go --></span></p>
<p> <b>Pin 7:</b> <span id="pin7"><!-- This is where the pin 7 reading will go --></span></p>
<p> <b>Pin 8:</b> <span id="pin8"><!-- This is where the pin 8 reading will go --></span></p>
<p> <b>Pin 9:</b> <span id="pin9"><!-- This is where the pin 9 reading will go --></span></p>
<p> <b>Voltage:</b> <span id="vin"><!-- This is where the voltage reading will go --></span></p>

关于javascript - Google Apps 脚本 Web 应用程序 AJAX、jQuery、JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23530516/

相关文章:

javascript - 在图像之间淡入淡出

javascript - 更改 CSS 元素的按钮

javascript - Wordpress 自定义查询分页和 Ajax

javascript - Mozilla 开发者网络 - 问号指的是什么?

javascript - 收到 AJAX 内容时 $.each 的下一次迭代

javascript - 为所有子项添加事件监听器

javascript - jQuery:移动窗口(或 FIFO)类型 DIV?

javascript - 等到所有 jQuery Ajax 请求都完成?

javascript - 如何计算localStorage中的总量和总计?

javascript - 使用 PreventDefault 函数重新加载表单提交