Post topics, source code that relate to the Arduino Platform

User avatar
By xoxxxoxo
#8781 I want my browser to get data from arduino every 5 second using javascript/json. When the browser with javascript made the connection to arduino, the arduino replied the json

{"b1":"1234"}

but the browser was not able to read it. I have tested the javascript to read the same json data in a file from my computer and it works, but if i change the address to arduino ip, it wont work. Is there any specific header required my the script in order to read it? (i have tried to include " = "HTTP/1.1 200 OK" and everything else but it is not working. Please help......

here is my javascript,

setInterval(function(){
show();
}, 5000);


function show()
{

$.get( 'http://192.168.1.104/!0Z', function( data ) {


$('#B1').show(); // 2pm
$('#b1').html( data.b1 ).show(); // 2pm

}, "json");

}
User avatar
By xoxxxoxo
#8846 This is the arduino code that reply the json.

content=("{\"b1\":\"1234\"}");
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type:application/json\r\nAccess-Control-Allow-Origin: *\r\nCache-Control: no-cache\r\n\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader +="Connection: close\r\n\r\n";
MESSAGE = httpHeader + content;

dbgSerial.print("AT+CIPSEND=0,");
dbgSerial.println(MESSAGE.length()+2);
dbgSerial.println(MESSAGE);
User avatar
By Tomer
#8849
xoxxxoxo wrote:This is the arduino code that reply the json.

content=("{\"b1\":\"1234\"}");
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type:application/json\r\nAccess-Control-Allow-Origin: *\r\nCache-Control: no-cache\r\n\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader +="Connection: close\r\n\r\n";
MESSAGE = httpHeader + content;

dbgSerial.print("AT+CIPSEND=0,");
dbgSerial.println(MESSAGE.length()+2);
dbgSerial.println(MESSAGE);


You have an extra \r\n in the response (right after the cache-control header), this causes the browser the parse the rest of the headers as the body, which is not a valid json.