Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

455
Vistas
myobj = JSON.parse(this.responseText); give an error in my project

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.
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

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>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda