0

对于一个学校项目,我正在使用 Arduino Yùn 模块。我有一个问题,但我无法在任何地方找到解决方案。

我正在向 Arduino 发送 HTTP GET 请求。Arduino 完美接收请求,因此打开 LED 并返回数据的获取请求确实会打开 LED,但无法正确返回。在我的 JavaScript 控制台中,我收到错误: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..

这真的很令人沮丧,我似乎无法让它发挥作用。

当我的计算机连接到 Arduino Yùn 自己的本地网络时,它应该一切正常,但我想将 Arduino 连接到我的家庭网络,然后发送请求。我错过了“访问控制允许来源”的正确授权。

什么是解决方案?

我尝试将其添加到我的 Arduino 的 C 代码中:

//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);

但是,即使我添加了此标头,我也会不断收到错误消息。

我无法理解可能出现的问题以及可能的解决方案。我真的希望有人能用他们的知识启发我!

为了完整起见,我如何拨打电话(永远不会成功):

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!");
        });

更简单的版本给出了同样的错误:

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

0 回答 0