Search code examples
jqueryjsonarduinoesp8266arduino-esp8266

arduino esp8266webserver error in returning json


In my arduino sketch I have this code:

void handleRoot() {
  server.send ( 200, "application/json", "{success:true,deviceID:'bla'}" );
}

so my arduino gets ip from my wifi AP(192.168.0.100) and in my js code of the client I have this:

$.ajax({
  type: "POST",
  url: "http://192.168.0.100/",
  success: function(responseData) {
        alert(responseData);
},
});

but alert(responseData) don't even fire up... I check in the browser console and I see that I getting this:

response headers:

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 29
Connection: close
Access-Control-Allow-Origin: *

and in the response content I have this:

{success:true,deviceID:'bla'}

why isn't this content not appear in the alert?


Solution

  • Use below samples :

    server.send(200, "text/plain", "{\"success\":1,\"deviceID\":\"bla\"}");
    

    and parse at the Ajax side :

    JSON.parse(responseData);