uHttpd/Yun Cross-Domain (Access-Control-Allow-Origin)

Hi,
i am facing (as expected) the CORS issue with the Yun. Has anybody solved this issue with uHttpd on the Yun side?
One possible solution is to add a header in the REST module e.g. "Access-Control-Allow-Origin: *" or getting the uHttpd to send out the header on any request. But the documentation of uHttpd is not very well :slight_smile:

Cheers

Hi

are you facing the problem using AJAX? If so JSONP may help:
http://www.lucadentella.it/2013/07/11/javascript-same-origin-policy-e-jsonp/

Hi,

Many thanks for your reply.

An example jquery ajax call with type of JSONP

$.ajax({
	url: "http://<your address>/data/put/<key>/<value>",
	dataType: "jsonp",
	error: function( jqXHR, textStatus, errorThrown )
		{
			console.log(textStatus);
		}
	}).done(function() {
		console.log("ok");
});

Is working but throws an error because the return is a json object instead of a tag.

A getJSON have the same issue with the origin:

$.getJSON( "http://<your address>/data/put/<key>/<value>", function( data ) {
	console.log(data)
});

Hopefully i am not totally wrong. Please correct me if so :slight_smile:

Cheers

Hi

you need also to change the "client" side of your app (running I suppose on the ATMEGA32u4) because of the call must return a "procedure" object that is the json data surrounded by the name of the JSONP function.

It is simpler to hard-code that name in your JSONP call, see the following example:
http://www.lucadentella.it/2013/08/07/ambientmonitor/

miliamber, by default jquery query string parameter for jsonp is "callback" while the lua code behind uhttpd expects it to be "jsonp". Try adding a

jsonp: 'jsonp'

to your ajax call
See jQuery.ajax() | jQuery API Documentation

Many Thanks Federico :slight_smile:
That's the solution!

$.ajax({
	url: "http://<address>/data/put/<key>/<value>",
	dataType: "jsonp",
	jsonp: 'jsonp',
}).done(function(e) {
	 console.log(e);
});