I will begin with a little code snippet so it will help me jog my memory for future endeavors.
client.fastrprintln(F('HTTP/2.0 200 OK'));
client.fastrprintln(F('Content-Type:text/html'));
client.fastrprintln(F("")); //tells that headers have ended and the body is starting
client.fastrprintln(F("calculate('{\"arduino\":1}')"));
The last line is equivalent to God making life. Why would I say that? Well for that we will have to see the whole picture and where and how I came here.
The need was simple, I was making a home automation application on phonegap and every arduino was on a different ip. Now we all know that cross domain access can only be done by jsonp in $.ajax which is given in jquery ( bless those ppl ). Why would I need cross domain you ask? Well my arduino is UNO and UNO means shitty small memory and for everyone's happiness we need the system to be fast, so why not remove the SD card dependency altogether meaning why not just make ajax calls to the arduino and have a html,css and js reside in the android / iphone device. When this happens you get into a problem. Android device has it's own ip, arduino UNOs have their own ips, so the need of XHR comes into play. Now we all know that XHR can't be accessed through cross domains so we require jsonp on them(what about websockets?). JSONP are like fetching javascripts from other sites which we do every single day but and there is a huge butt here these scripts are transformed into json objects. So in short through jsonp on $.ajax we fetch the information in the form of scripts but we run it through a json parser and lo and behold we get a json object. Now this is all very simple on the android / iphone side with the help of Phonegap i.e. calling to a cross domain to give us a jsonp. Here is the code:
url:"http://192.168.0.105/hi",
dataType:'jsonp',
jsonpCallback:'calculate',
success:function(data)
{
console.log("success");
console.log(data);
}
});
Now you see that I have added the jsonpCallback as calculate. If you revisit the first code i wrote, which was in arduino, you see what I am sending the the response data. You got it I am sending a whole function back in the response data calculate('{\"arduino\":1}'). When this function returns to the ajax it sees that the callback has returned from the server side with data ("arduino":1). this data can then be used by our html, js to change a part of the html. I searched the net for an answer on this but found none, so I am here sharing this with you. So if you need a cross domain response from the arduino in the form of a json object this is your answer. Hope this helps anyone who reads it ^^
client.fastrprintln(F('HTTP/2.0 200 OK'));
client.fastrprintln(F('Content-Type:text/html'));
client.fastrprintln(F("")); //tells that headers have ended and the body is starting
client.fastrprintln(F("calculate('{\"arduino\":1}')"));
The last line is equivalent to God making life. Why would I say that? Well for that we will have to see the whole picture and where and how I came here.
The need was simple, I was making a home automation application on phonegap and every arduino was on a different ip. Now we all know that cross domain access can only be done by jsonp in $.ajax which is given in jquery ( bless those ppl ). Why would I need cross domain you ask? Well my arduino is UNO and UNO means shitty small memory and for everyone's happiness we need the system to be fast, so why not remove the SD card dependency altogether meaning why not just make ajax calls to the arduino and have a html,css and js reside in the android / iphone device. When this happens you get into a problem. Android device has it's own ip, arduino UNOs have their own ips, so the need of XHR comes into play. Now we all know that XHR can't be accessed through cross domains so we require jsonp on them(what about websockets?). JSONP are like fetching javascripts from other sites which we do every single day but and there is a huge butt here these scripts are transformed into json objects. So in short through jsonp on $.ajax we fetch the information in the form of scripts but we run it through a json parser and lo and behold we get a json object. Now this is all very simple on the android / iphone side with the help of Phonegap i.e. calling to a cross domain to give us a jsonp. Here is the code:
$.ajax({type:"GET",
url:"http://192.168.0.105/hi",
dataType:'jsonp',
jsonpCallback:'calculate',
success:function(data)
{
console.log("success");
console.log(data);
}
});
Now you see that I have added the jsonpCallback as calculate. If you revisit the first code i wrote, which was in arduino, you see what I am sending the the response data. You got it I am sending a whole function back in the response data calculate('{\"arduino\":1}'). When this function returns to the ajax it sees that the callback has returned from the server side with data ("arduino":1). this data can then be used by our html, js to change a part of the html. I searched the net for an answer on this but found none, so I am here sharing this with you. So if you need a cross domain response from the arduino in the form of a json object this is your answer. Hope this helps anyone who reads it ^^
Gracias!!! Es lo que llevaba días intentando hacer! :)
ReplyDeleteSarita did you even understand the logic behind what I said?
DeleteReliable solutions for electronic hardware development. Metal Core PCB Solutions
ReplyDelete