REST calls from Angular.js to Arduino Yùn not authorized - Access-Control-Allow-Origin (Wi-Fi)

398 views Asked by At

For a school project I'm working with an Arduino Yùn module. I have one problem though for which I can't find a solution anywhere.

I'm sending HTTP GET requests to the Arduino. The Arduino receives the requests perfectly, so a get request to turn on an LED and return data does turn on the LED, but it isn't able to return correctly. In my JavaScript console I get the error: XMLHttpRequest cannot load http://192.168.0.234/arduino/digital/3/0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access..

This is really frustrating and I can't seem to get it work.

When my computer is connected to the Arduino Yùn's own local network it should all work fine, but I want to connect the Arduino to my home network and then send the requests. I am missing the right authorization though for the 'Access-Control-Allow-Origin'.

What is a soltion for this?

I have tried adding this to the C code of my Arduino:

//headers
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("Access-Control-Allow-Origin: *");
client.println();

// Send feedback to client
client.print(F("Pin D"));
client.print(pin);
client.print(F(" set to "));
client.println(value);

I keep getting the error though, even if I add this header.

I can't get my head around what could possibly be wrong and what could possibly be the solution. I really hope someone can enlighten me with their knowledge!

For completeness, how I do my calls (the success is never run):

var req = {
            method: 'GET',
            url: 'http://192.168.0.234/arduino/digital/' + lednr + '/' + status,
            headers: {
                'Content-Type': 'application/javascript'
            }
        };

        $http(req).success(function() {
            alert("GREAT SUCCESS!");
        });

More simple version gives same error:

$http.get("http://192.168.0.234/arduino/digital/" + lednr + "/" + status);
0

There are 0 answers