I have a Raspberry Pi acting as a WiFi access point and serving pages to my iPhone from code using the Python Tornado framework. I have now connected my Yun to this WiFi network. I wish to make AJAX calls into the Yun from the Raspberry Pi hosted pages.
This works OK if I use the /arduino endpoint via YunClient/YunServer as you can write your own headers out. AJAX requires the 'Access-Control-Allow-Origin:' header to be correctly set to allow a call from a page hosted on one machine to call into another and respond.
My requirement is very simple and I would like to use the /data endpoint. This allows the 32U4 code to simply do the odd 'Bridge.put(..);' call when required.
I can't for the life of me see which piece of python code on the Linux side writes the headers. I'm guessing in my case I need to simply add one line somewhere?
I'm well aware of JSONP and it's nice to know the serversupports it. I hadn't found any reference to the Yun supporting it before. I did find it a little odd in that it doesn't strictly do what most tell you the server hack should do. It not not only 'pads' the data in the callback but pads the JSON data with quotes as well. Not difficult to fix but contrary to almost every example I have.
Back to the original question though. CORS is more modern and certainly a more desirable a solution these days than the older JSONP hack. All my current web logic and other servers use it so I don't want to have to rewrite things just to use the Yun. I can use CORS if I use the '/arduino' endpoint (via writing out my own headers) and it would be nice to use it on the '/data' endpoint as well.
wildpalms:
I'm well aware of JSONP and it's nice to know the serversupports it. I hadn't found any reference to the Yun supporting it before. I did find it a little odd in that it doesn't strictly do what most tell you the server hack should do. It not not only 'pads' the data in the callback but pads the JSON data with quotes as well. Not difficult to fix but contrary to almost every example I have.
Can you add details? What do you expect and what do you get? (if possible show code or json snippets or whatever helps understanding)
wildpalms:
Back to the original question though. CORS is more modern and certainly a more desirable a solution these days than the older JSONP hack. All my current web logic and other servers use it so I don't want to have to rewrite things just to use the Yun. I can use CORS if I use the '/arduino' endpoint (via writing out my own headers) and it would be nice to use it on the '/data' endpoint as well.
The following Javascript is a JQuery JSONP AJAX call that matches the JQuery documention.
var addr = 'http://192.168.2.103:8080';
var path = '/data/get/id?callback=?';
$.getJSON(addr + path, function(response) {
$("#log").append(response.value, ', ');
});
Run against my server I get the following if I check the console in Chrome:
jQuery111105559559187386185_1409322963115({"value": "0", "key": "id", "response": "get"})
This matches the expected response from every piece of documentation I checked and response.value successfully reads the value of 'id'.
The following is the same JQuery JSONP AJAX call made to the Yun.
var addr = 'http://192.168.42.50';
var path = '/data/get/id?callback=?';
$.getJSON(addr + path, function(response) {
$("#log").append(response.value, ', ');
});
As you can see not only has the server added the callback (successfully) but it quoted the JSON. The same Javascript no longer treats this as JSON data so 'response.value' has no value. The Javascript has to be modified to parse the string back into JSON.
Thank you. I'll go take a look.
I had said in my first post that I had used '/arduino' with custom headers successfully already. What I was after was to do the same with '/data'
I've solved the problem for now by taking control of ttyATH0 and using Tornado & PySerial to implement the same functionality but thanks for your help.