Arduino Ethernet Access-Control-Allow-Origin问题

时间:2014-02-03 19:15:07

标签: c++ ajax arduino cors access-control

嘿所有我想发布数据到arduino ethernet microprocesser但是当我尝试使用Jquery AJAX在我的应用程序中调用该函数时它似乎无效。

这是ajax代码:

function sendToArduinoEthernet(whatsBeingSent) {
    jQuery.support.cors = true;

    $.ajax({
        crossDomain: true,
        async : true,
        type: "POST",
        headers: {
            'Access-Control-Allow-Origin': '*'
        },
        url: "http://192.168.9.199:199/?r=" + whatsBeingSent,
        data: '{}',
        contentType: "text/html",
        dataType: "html",
        success: OnSuccessCallArduino,
        error: OnErrorCallArduino
    });      
}

这是Arduino代码:

#include <Ethernet.h>
#include <SPI.h>

boolean reading = false;
char c;
EthernetClient client;
String readString = String(30); //string for fetching data from address

///////////////////////////////////////////////////////////////////////////////////////////
//Ethernet CONFIGURE
///////////////////////////////////////////////////////////////////////////////////////////
  byte ip[] = { 192, 168, 9, 199 };   //Manual setup only 421
  byte gateway[] = { 192, 168, 9, 1 }; //Manual setup only
  byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // if need to change the MAC address (Very Rare)
  EthernetServer server = EthernetServer(199); //port 199
///////////////////////////////////////////////////////////////////////////////////////////
//CONFIGURE for relays
///////////////////////////////////////////////////////////////////////////////////////////
  //(analog->digital output)
  int relay1 = 14; //Android phone USB power (turn on to shut off power to phone)
  int relay2 = 15; //Bluray eject
  int relay3 = 16; //PC power button
  int relay4 = 17; //Android Phone Power button

void setup(){
    Serial.begin(9600);

    pinMode(relay1, OUTPUT);
    pinMode(relay2, OUTPUT);
    pinMode(relay3, OUTPUT);
    pinMode(relay4, OUTPUT);
    pinMode(fan1, OUTPUT);
    pinMode(fan2, OUTPUT);

    analogWrite(fan1, pwmVal);
    analogWrite(fan2, pwmVal);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);

    Ethernet.begin(mac, ip, gateway, subnet); //for manual setup  
    server.begin();
    Serial.println(Ethernet.localIP());
}

void loop(){
    checkForClient();
}

void checkForClient(){
    client = server.available();

    if (client) {
      boolean currentLineIsBlank = true;
      boolean sentHeader = false;

      while (client.connected()) {
          if (client.available()) {  
              if(!sentHeader){
                // send a standard http response header
                client.println("HTTP/1.1 200 OK");
                client.println("Content-Type: text/html");
                sentHeader = true;
              }

              c = client.read();

              if (readString.length() < 16)
              {
                readString += String(c);
              }

              if(reading && c == ' ') reading = false;
              if(c == '?') reading = true; //found the ?, begin reading the info 
              if (c == '\n' && currentLineIsBlank) break;

              if (c == '\n') {
                currentLineIsBlank = true;          
              }else if (c != '\r') {
                currentLineIsBlank = false;
              }
          }
      }  
      delay(1);   
      switchStuff(readString);
      client.stop();
      readString = "";
      c = ' ';
      reading = false;
    }
}

void switchStuff(String stuff) {
  stuff.toLowerCase();
  //client.println(stuff);

  if (stuff.indexOf("r1on") > 0) {    
    digitalWrite(relay1, LOW); //Turn 5v USB power ON
  } else if (stuff.indexOf("r1off") > 0) {
    digitalWrite(relay1, HIGH); //Turn 5v USB power OFF
  } else if (stuff.indexOf("r2") > 0) {
    digitalWrite(relay2, LOW); //Eject bluray Disc
    delay(500); //pause for 1/2 second
    digitalWrite(relay2, HIGH);
  } else if (stuff.indexOf("r3") > 0) {
    digitalWrite(relay3, LOW); //"Push" the pc power button
    delay(500); //pause for 1/2 second
    digitalWrite(relay3, HIGH); //"Release" the pc power button
  } else if (stuff.indexOf("r4") > 0) {
    digitalWrite(relay4, LOW); //"Push" phone power button down
    delay(7000); //pause for 7 seconds
    digitalWrite(relay4, HIGH); //"Relese" phone power button
  } else if (stuff.indexOf("r5") > 0) {
    digitalWrite(relay4, LOW); //"Push" phone power button down
    delay(500); //pause for 1/2 second
    digitalWrite(relay4, HIGH); //"Relese" phone power button
  }
}

1 个答案:

答案 0 :(得分:1)

这是因为HTTP服务器必须发送Access-Control-Allow-Origin标头。

Cross-Origin Resource Sharing是每个最近的Web浏览器中嵌入的安全功能,它定义了浏览器和服务器可以交互以确定是否允许跨源请求的方式。

您对服务器公开的资源的访问权限取决于他在对您的请求的响应中定义的CORS政策。因此,您可能希望返回此标头服务器端,而不是在请求客户端使用它。如果不这样做,将导致浏览器发出错误,指出您无权访问给定资源。

您可以找到更多详细信息以及此here的直接示例。