how to implement CORS with webserver using ethernet library

I'm trying to get rid of the CORS error produced within Chrome while accessing a webserver on the arduino.
I have been attempting to use the webserver application that comes with the ethernet library. I am sending and receiving data to the arduino webserver using javascript under Chrome. To remedy this this error I am using a Chrome plugin (called Allow CORS) where I can disable this in error Chrome. I would like to know if it's feasible to implement a change in the Arduino Webserver application that would support CORS (my program is only within a local network).

While I'm trying both the 5100 and the 5500 sheilds, I am not using the sdcard for files - I'm simply taking the request, processing an answer and outputting that answer via http on the sheild ethernet port. I'm guessing that one possibility might be to use the sdcard for files and then maybe put a "Access-Control-Allow-Origin: *" in a .htaccess file - although I don't really need any files since I'm just using the webserver to get data that I process, and then send back to the client.

Any input or suggestions would be welcome.

I'm not sure the reason, but it appears possible to just put the code header of the page like so;
client.println("HTTP/1.1 200 OK");
//client.println("Content-Type: text/html");
client.println("Access-Control-Allow-Origin: *");
//client.println("Connection: close");
// client.print("This will be the first thing to appear on the browser.");

I experimented commenting out the above lines - the above is the absolute minimum required to remove the CORS error. Surprisingly, the fourth line which presumably just prints a carriage return IS necessary. Removing that line will not produce the CORS error but no content will appear in the webpage without it.

client.println("Access-Control-Allow-Origin: *");
is everything you need for CORS

empty line separates headers and body in HTTP protocol

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.