我在玩Ecobee,并试图写一个网页,人们可以在那里为我的应用程序获取一个PIN。我必须遗漏一些东西,bc,当我在CodeRunner中运行下面的代码时,什么都不会发生。有人能指出我做错了什么吗?显然,我想让它格式化并使它看起来很好看,但是首先我需要能够检索数据。非常感谢。
麦克
<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"}
<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>发布于 2021-06-26 07:55:32
实际上,您必须调用您的函数。另一个问题是,.error()在当前jQuery版本中不再作为延迟对象的方法存在:“jqXHR.success()、jqXHR.error()和jqXHR.complete()回调方法在jQuery 3.0时被删除”。我用的是.fail()。
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()<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>
发布于 2021-06-26 08:12:10
卡斯滕的回答是正确的,不过我还要补充一点:
您可以使用JQuery的本机$(function PINCode() {...})。
它立即调用函数,同时使用速度更快的$.ready。
在这里阅读更多信息:Difference between onload() and $.ready?
https://stackoverflow.com/questions/68140234
复制相似问题