2

I'm trying to manipulate the attributes of an object (access) using a variable, but I'm not getting it.

I tried to do the following:

$('#extruderAtual').text(obj.Printer1.extruder[0].tempRead);

basically it only changes the text of a given 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);

    });
  }
}

It's not insteressante to know how I get these variables, but how can I use them. In the variable url, I get the following link:

http://localhost:3344/printer/api/Printer1?apikey=e2ddc036-f883-460e-beb6-fc41026d720d&a=stateList&data={}

which in turn, returns something like this:

{"Printer1":{"activeExtruder":0,"debugLevel":6,"extruder":[{"error":0,"output":0,"tempRead":20.000000000000000,"tempSet":0.0000000000000000}]

my idea is to do something like this:

 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

    });
  }
}

But it does not work

Obs(it is not interesting to know how the variable printerName receives some value, in this code suppose that it has the same name as the link and passed: "Printer1")

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • 2
    Your question is unclear. – random Jun 12 '19 at 12:20
  • 2
    Please explain `it does not work`. `it only changes the text of a given id` is not clear enough what it means, since the code shown has you select and update the text of the node with id `#extruderAtual`. What are you expecting? – Shilly Jun 12 '19 at 12:21
  • I want to be able to pass a variable as an attribute of var obj = JSON.parse (msg); because for me to access "extruder" that is in: {"Printer1": {"activeExtruder": 0, "debugLevel": 6, "extruder": {"error": 0, "output": 0, "tempRead": 20.000000000000000, "tempSet": 0.0000000000000000} I need to do the following: $ ('# extruderAtual'). text (obj.Printer1.extruder [0] .tempRead); – Eng Carvalho Jun 12 '19 at 12:42
  • But I do not want to, because your I need to change the name of the printer, I will have to change the whole code, so I wanted something like this: $ ('# extruderAtual'). text (obj.printerName.extruder [0] .tempRead); printerName is the variable where the printer name is saved, #extruderAtual is an input where the user passes the name – Eng Carvalho Jun 12 '19 at 12:43
  • 2
    `obj[printerName].extruder[0]...` – James Jun 12 '19 at 12:43
  • I was supposed to answer this question, I don't know what's gotten into mods these days. `obj[printerName].extruder[0].tempRead` https://jsfiddle.net/Lkpva1sw/ – Aivan Monceller Jun 12 '19 at 12:48
  • It worked great, thank you very much for the help. – Eng Carvalho Jun 12 '19 at 18:50

0 Answers0