Hey y'all,
Im am running a debug client in google chrome using tomcat and eclipse.
I also have an Arduino Uno with an Ethernet shield as a server. Also, my client
is written in java using GWT (Google Web Toolkit). When using
my debug client to send an API to my arduino server, i encounter to issues. 1) I get the below highlighted error 2) response.getStatusCode() r returns 0. I was wondering if someone could assist?
XMLHttpRequest cannot load http://arduinoServerAPIUSingItsIPADDY. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8888' is therefore not allowed access.
Below is my java client code and my server code on my Arduino:
Java Client code:
RequestBuilder rb = new RequestBuilder(RequestBuilder.POST,
"http://xxx.xxx.xxx.MyArduinoSeverIPAddy");
rb.setHeader("Content-Type", "application/json");
try {
rb.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
System.out.println("Plan fail");
}
public void onResponseReceived(Request request,
Response response) {
System.out.println(response.getText());
if (200 == response.getStatusCode()) {
System.out.println("IT WORKED");
if(response.getText() == "ON")
System.out.println("ON");
else
System.out.println("OFF");
} else {
System.out.println("IT REPLIED BUT DIDNT WORK");
}
}
});
} catch (RequestException e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
System.out.println(e.getCause());
}
Arduino server code sending json:
#include <SPI.h>
#include <Ethernet.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xDE, 0xDE, 0xDE, 0xDE, 0xDE };
IPAddress ip(ARDUINOIPADDY); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
boolean incoming = 0;
String HTTP_req; // stores the HTTP request
boolean LED_status = 0; // state of LED, off by default
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
pinMode(2, OUTPUT); // LED on pin 2
}
void loop(void) {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json;charset=utf-8");
client.println("Server: Arduino");
client.println("Connnection: close");
client.println();
client.print("{\"arduino\":[{\"location\":\"indoor\",\"celsius\":\"");
client.print("00.0");
client.print("\"},");
client.print("{\"location\":\"outdoor\",\"celsius\":\"");
client.print("0.333");
client.print("\"}]}");
client.println();
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}