-1

I check other similar questions but i really cant understand my case.

I need to get sensors values from json and put them in html. This is the html code.

<div class="sensors">
            <div class="sensor">
                <p class="sensor_title">Температура воздуха</p>
                <p class="sensor_value" id="sensor_value1">123,45</p>
                <p class="sensor_measure">С</p>
            </div>
            <div class="sensor">
                <p class="sensor_title">Давление</p>
                <p class="sensor_value" id="sensor_value2">123.45</p>
                <p class="sensor_measure">hPa</p>
            </div>
            <div class="sensor">
                <p class="sensor_title">Влажность</p>
                <p class="sensor_value" id="sensor_value3">300</p>
                <p class="sensor_measure">%</p>
            </div>
            <div class="sensor">
                <p class="sensor_title">Уровень освещенности</p>
                <p class="sensor_value" id="sensor_value4">400</p>
                <p class="sensor_measure">Lx</p>
            </div>
            <div class="sensor">
                <p class="sensor_title">Уровень воды</p>
                <p class="sensor_value1" id="sensor_value6">Проверка</p>                    
                <p class="sensor_measure"></p>
            </div>
        </div>

And this is the js function(xmlHttp is just a variable for xmlHttprequest).

load_sensors();

function load_sensors(){
            if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
                xmlHttp.open('PUT','/configsSensors.json',true);                    
                xmlHttp.onload = function(e) {
                    jsonResponse=JSON.parse(xmlHttp.responseText);
                    document.getElementById('sensor_value1').innerHTML=jsonResponse.sensor_value1;
                    document.getElementById('sensor_value2').innerHTML=jsonResponse.sensor_value2;
                    document.getElementById('sensor_value3').innerHTML=jsonResponse.sensor_value3;
                    document.getElementById('sensor_value4').innerHTML=jsonResponse.sensor_value4;
                    document.getElementById('sensor_value5').innerHTML=jsonResponse.sensor_value5;
                    if(jsonResponse.sensor_value6=="1"){
                        document.getElementById('sensor_value6').innerHTML="Нормальный";
                    }else{
                        document.getElementById('sensor_value6').innerHTML="Критический";
                    }
                }
            };
            xmlHttp.send();
        }

This is the json data.

{","sensor_value1":"nan",
   "sensor_value2":"nan",
   "sensor_value3":"nan",
   "sensor_value4":"-2.00",
   "sensor_value5":"",
   "sensor_value6":"1"}

I trying to fix this maybe two weeks already. Please tell me what exactly i need to write here :(

1 Answers1

0

It's giving you that error because you have the leading ", string after the {, eliminate the code that writes the charachters ", to the json and you should resolve the error.

Kevin Spaghetti
  • 620
  • 4
  • 16
  • it's the `",` after the `{` that are at fault, not the "leading `","`" - since the leading characters are `{","` and the second `"` is required – Jaromanda X Dec 24 '19 at 13:11