-->
Page 1 of 1

My browser unable to read the json data from arduino

PostPosted: Mon Feb 02, 2015 10:53 am
by xoxxxoxo
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");

}

Re: My browser unable to read the json data from arduino

PostPosted: Mon Feb 02, 2015 9:18 pm
by SoftwareDev
What is your exact HTTP reply to the browser? Include all characters e.g. \r, \n

Re: My browser unable to read the json data from arduino

PostPosted: Tue Feb 03, 2015 7:41 am
by xoxxxoxo
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);

Re: My browser unable to read the json data from arduino

PostPosted: Tue Feb 03, 2015 8:27 am
by Tomer
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.