0

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);
E. V. d. B.
  • 815
  • 2
  • 13
  • 30
  • Are you using Chrome? – Avraam Mavridis Dec 08 '14 at 22:33
  • You could try using a CORS plugin for chrome - https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en – Gillespie Dec 08 '14 at 22:34
  • I am in fact using Chrome but the website is meant to work on any device any browser (that supports JS of course), but I don't think I can access JavaScript console on Safari for iOS to check if it works. Why would it give this error on chrome? Haven't tried another browser yet. I'll check the plugin, thanks! But I still need it to work on mobile without plugin – E. V. d. B. Dec 08 '14 at 22:36
  • Can you verify that you are getting the CORS header in Chrome's web console? – Gillespie Dec 08 '14 at 22:37
  • Or you can relax the Chrome's security, and maybe try with `application/json' and/or `jsonp` instead of get – Avraam Mavridis Dec 08 '14 at 22:38
  • @RPGillespie where in chrome can I check that info? – E. V. d. B. Dec 08 '14 at 22:39
  • @Avraam what do you man with 'instead of get', I think I need to do it with a get, no? Do you mean just change the Content-Type? I heard application/javascript is jsonp and application/json is classic json. – E. V. d. B. Dec 08 '14 at 22:40
  • 1
    From chrome's menu open developer tools. Should look like this after loading your page: http://i.imgur.com/UGzT9JK.png . It's under Network -> Page -> Headers. – Gillespie Dec 08 '14 at 22:41
  • @E.V.d.B. there is a method on angular for jsonp, `jsonp(url, [config]);` Shortcut method to perform JSONP request. You can give it a try. – Avraam Mavridis Dec 08 '14 at 22:43
  • @RPGillespie Nope the header is not present. i'm starting to think the arduino data sent is not in JSONP format but I don't know if that influences if headers are given right or not... http://i.imgur.com/YbbWBEU.png @ Avraam I'm gonna take a look right now, thanks – E. V. d. B. Dec 08 '14 at 22:47
  • It is actually working now! I'll post my exact solution later but basically I fiddled a bit. Only problem is that if I call the built-in api under `/data/get` to get the datastore I can't give it the header because it's a built-in function but I'll find something! – E. V. d. B. Dec 08 '14 at 23:03
  • 1
    @E.V.d.B. could you post the solution? – tomsoft Aug 29 '15 at 14:44

0 Answers0