Soy novato en javascript y tengo un problema para analizar Json
Leí la pregunta similar pero ninguno de ellos me da una respuesta correcta.
en mi código hay funciones a continuación que deben analizar un archivo json llamado wifi.txt cuando se carga la página.
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(); el problema es que cuando la página carga el código myobj = JSON.parse(this.responseText); no funciona y su código a continuación tampoco funciona.
Intento consol.log y alert("test") en cada línea del código principal, pero solo codigo antes myobj = JSON.parse(this.responseText); trabajar correctamente
Obtuve el siguiente error
Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.responsewifimyobj es una var
I code this for a NODEMCU ESP8266 project.gracias F. Müller
Escribo esta respuesta para quien tiene este problema como yo
Intenté durante aproximadamente 2 días resolver este problema.
como F. Müller dijo que el problema era encontrar un archivo json correcto
Me di cuenta de que en la memoria spiffs de esp8266 hay una barra inclinada / antes del nombre de los archivos
aquí tengo que escribir regwifi.open("get","/wifi.txt",true); en lugar de regwifi.open("get","wifi.txt",true); en la función processwificonfig()
y eso es
Intenta trabajar con esto... utilízalo como punto de partida:
<!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>