我正在尝试使用变量来操纵对象(访问)的属性,但我没有得到它。
我尝试执行以下操作:
$('#extruderAtual').text(obj.Printer1.extruder[0].tempRead);
基本上,它只会更改给定ID的文本
else {
var host = $('#host').val();
var printerName = $('#namePrinter').val();
var key = $('#key').val();
var url = host+'/printer/api/'+printerName+'apikey='+key+'&a=stateList&data={}';
$.ajax({
type: "GET",
url: url,
success: function (msg) {
var obj = JSON.parse(msg);
});
}
}
知道如何获得这些变量并不是明智之举,而是如何使用它们。 在变量url中,获得以下链接:
http://localhost:3344/printer/api/Printer1?apikey=e2ddc036-f883-460e-beb6-fc41026d720d&a=stateList&data={}
依次返回如下内容:
{"Printer1":{"activeExtruder":0,"debugLevel":6,"extruder":[{"error":0,"output":0,"tempRead":20.000000000000000,"tempSet":0.0000000000000000}]
我的想法是做这样的事情:
else {
var host = $('#host').val();
var printerName = $('#namePrinter').val();
var key = $('#key').val();
var url = host+'/printer/api/'+printerName+'apikey='+key+'&a=stateList&data={}';
$.ajax({
type: "GET",
url: url,
success: function (msg) {
var obj = JSON.parse(msg);
var printerName = $('#namePrinter').val();//I declare this variable again
alert(obj.printerName.extruder[0].tempRead);//give an alert only to show the expected integer value
});
}
}
但这不起作用
糟糕(不知道变量printerName是如何接收某个值的,在此代码中假定它与链接具有相同的名称并传递给它:“ Printer1”)