Home > OS >  myobj = JSON.parse(this.responseText); give an error in my project
myobj = JSON.parse(this.responseText); give an error in my project

Time:02-15

I'm newbie in javascript and I have a problem to parse Json

I read the similar question but none of them give me a correct answers.

in my code there are below functions that should parse a json file called wifi.txt when page loading.

function responsewifi(){
    if(this.readyState==4 && this.status ==200){
        myobj = JSON.parse(this.responseText);
        
        document.getElementById("SSID").value = myobj.SSID;
        document.getElementById("APKEY").value = myobj.APKEY;
        document.getElementById("ESPAPNAME").value = myobj.ESPAPNAME;
        document.getElementById("ESPAPKEY").value = myobj.ESPAPKEY;
        var wifimodes = myobj.wifimode;
        if(wifimodes=="STAmode"){document.getElementById("STAmode").checked = true;}
        if(wifimodes=="APmode"){document.getElementById("APmode").checked = true;}
        if(wifimodes=="MULTImode"){document.getElementById("MULTImode").checked = true;}
    }
}
function processwificonfig()
{
    regwifi.open("get","wifi.txt",true);
    regwifi.onreadystatechange = responsewifi;
    regwifi.send();
}

processwificonfig();

the problem is that when page load the code myobj = JSON.parse(this.responseText); doesn't work and its below code doesn't work too.

I try consol.log and alert("test") in every line of top code but just code before myobj = JSON.parse(this.responseText); work correctly

I get the following error

Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.responsewifi

myobj is a var


I code this for a NODEMCU ESP8266 project.

CodePudding user response:

Try working with this... use as starting point:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>test</title>
</head>
<body>

  <p id="SSID"></p>
  <p id="APKEY"></p>
  <p id="ESPAPNAME"></p>
  <p id="ESPAPKEY"></p>

  <script>
  const xhr = new XMLHttpRequest(),
  method = "GET",
  url = "wifi.txt";
  xhr.open(method, url, true);
  xhr.onreadystatechange = function () {
    if(xhr.readyState === XMLHttpRequest.DONE) {
      var status = xhr.status;
      if (status === 0 || (status >= 200 && status < 400)) {
        // The request has been completed successfully
        // console.log(xhr.responseText);
        myobj = JSON.parse(xhr.responseText);
        document.getElementById("SSID").innerText = myobj.SSID;
        document.getElementById("APKEY").innerText = myobj.APKEY;
        document.getElementById("ESPAPNAME").innerText = myobj.ESPAPNAME;
        document.getElementById("ESPAPKEY").innerText = myobj.ESPAPKEY;
        // wifimodes next...
      } else {
        console.log('Oh no! There has been an error with the request!');
      }
    }
  };
  xhr.send();
  </script>

 <!-- wifi.txt file content:
  {
    "SSID": "777",
    "APKEY": "333",
    "ESPAPNAME": "neo",
    "ESPAPKEY": "222"
  }
  -->

</body>
</html>

CodePudding user response:

thanks you F. Müller

I write this answer for who has this problem as me

I tried for about 2 days to solve this problem

as F. Müller said problem was finding a correct json file

I realized that in spiffs memory of esp8266 there is a slash/ before files name

here I have to write regwifi.open("get","/wifi.txt",true); instead of regwifi.open("get","wifi.txt",true); in processwificonfig() function

and thats it

  • Related