Changing headers for basic /data/get REST endpoint response

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?

Dave

Use JSONP (and google it if you don't know what it is). If you use jquery, google for "jquery jsonp example"

Thank you for your reply.

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.

Dave

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.

Everything but /arduino is managed by the webpanel, so fixes should go there GitHub - arduino/YunWebUI: Arduino/Genuino Yún or Yún Shield Web panel

/arduino is managed by the sketch and you can write custom headers from the sketch: see this topic for an example
http://forum.arduino.cc//index.php?topic=191895.0

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, ', ');
});

and here is the result in Chrome

jQuery111108308434812352061_1409321817509("{\u0022value\u0022:\u00221\u0022,\u0022key\u0022:\u0022id\u0022,\u0022response\u0022:\u0022put\u0022}");

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.

You're very right. Uhmm. Can you give a spin to the attached lua script? If it works, I'll push it into a new version of the webpanel.

In order to test it, download it on your computer, then copy it to the yun at path

/usr/lib/lua/luci/controller/arduino/index.lua

On linux/mac, you can open a terminal and type

scp index.lua root@192.168.42.50:/usr/lib/lua/luci/controller/arduino/index.lua

index.lua (26.6 KB)

That worked perfectly thank you Federico.

JSONP returned data as expected.

Dave

Thank you. I'm releasing the update to the public. It should become available in the next few days