首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript在HTML中并显示JSON响应

Javascript在HTML中并显示JSON响应
EN

Stack Overflow用户
提问于 2021-06-26 07:38:11
回答 2查看 74关注 0票数 0

我在玩Ecobee,并试图写一个网页,人们可以在那里为我的应用程序获取一个PIN。我必须遗漏一些东西,bc,当我在CodeRunner中运行下面的代码时,什么都不会发生。有人能指出我做错了什么吗?显然,我想让它格式化并使它看起来很好看,但是首先我需要能够检索数据。非常感谢。

麦克

代码语言:javascript
复制
<HTML>
<head>
    <title>Ecobee API PIN TEST</title>
</head>
<body onload="PINCode();">
    <script type="text/javascript">
        function PINCode() {
            
            apiKey = 'API_KEY'
            
            // set all apiKey inputs to have apiKey value entered
            $(".apiKey").each(function() {
                $(this).val(apiKey);
            });
            
            var url = 'https://api.ecobee.com/authorize?response_type=ecobeePin&client_id=' + apiKey + '&scope=smartWrite';
            
            $.getJSON(url,  function(data) {
                // set authCode to be code attribute of response
                $("#authCode").val(data.code);
                
                var response = JSON.stringify(data, null, 4);
                $('#response1').html(response);
            })
            .error(function(jqXHR, textStatus, errorThrown) {
                $('#response1').html("The following error was returned: <br><br>" + jqXHR.responseText) 
            });
        }
    </script>
    <pre id="response1" class="code-json">(Response JSON will appear here...I hope.)</pre>
</body>

这是我在卡斯滕投入之后的最新尝试。现在我正在为我的文本字段获得一个值,但是它是一个错误:{"readyState":0,“状态”:0,"statusText":"error"}

代码语言:javascript
复制
    <HTML>
<head>
    <title>Ecobee API PIN TEST</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <script type="text/javascript">
        $(document).ready(function PINCode() {
            
            apiKey = 'API_KEY'
            
            // set all apiKey inputs to have apiKey value entered
            $(".apiKey").each(function() {
                $(this).val(apiKey);
            });
            
            var url = 'https://api.ecobee.com/authorize?response_type=ecobeePin&client_id=' + apiKey + '&scope=smartWrite';
            $.getJSON(url,function(data) {
                // set authCode to be code attribute of response
                $("#authCode").val(data.code);
                alert("JSON Ran")
                var response = JSON.stringify(data, null, 4);
                $('#response1').html(response);
            })
            .fail(function(jqXHR, textStatus) {
                $('#response1').html("The following error was returned: <br><br>" + JSON.stringify(jqXHR) + "<p>Please Contact <b>M2 AV Consulting</b> at <a href=mailto:support@m2avc.com?subject='EcobeePINSupport'>support@m2avc.com</a>")
                console.log(textStatus);
            });
        })
    </script>
    <pre id="response1" class="code-json">(Response JSON will appear here...I hope.)</pre>
</body>
</HTML>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-26 07:55:32

实际上,您必须调用您的函数。另一个问题是,.error()在当前jQuery版本中不再作为延迟对象的方法存在:“jqXHR.success()、jqXHR.error()和jqXHR.complete()回调方法在jQuery 3.0时被删除”。我用的是.fail()

代码语言:javascript
复制
function PINCode() {
            
            apiKey='API_KEY'
            
            // set all apiKey inputs to have apiKey value entered
            $(".apiKey").each(function() {
                $(this).val(apiKey);
            });
            
            var url = 'https://api.ecobee.com/authorize?response_type=ecobeePin&client_id=' + apiKey + '&scope=smartWrite';
            
            $.getJSON(url,  function(data) {
                // set authCode to be code attribute of response
                $("#authCode").val(data.code);
                
                var response = JSON.stringify(data, null, 4);
                $('#response1').html(response);
            })
            .fail(function(jqXHR, textStatus) {
                $('#response1').html("The following error was returned: <br><br>" + JSON.stringify(jqXHR))
                console.log(textStatus);
            });
        }
        PINCode()
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<pre id="response1" class="code-json">(Response JSON will appear here...I hope.)</pre>

票数 0
EN

Stack Overflow用户

发布于 2021-06-26 08:12:10

卡斯滕的回答是正确的,不过我还要补充一点:

您可以使用JQuery的本机$(function PINCode() {...})

它立即调用函数,同时使用速度更快的$.ready

在这里阅读更多信息:Difference between onload() and $.ready?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68140234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档