You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
Just a quick note to fix CORS error especially for testing when you are developing your client (so local file on your computer) requesting on your ESP.
First solution, launching chromium without security (for exemple on linux) :
// called when the url is not defined
server.onNotFound([]() {
if (server.method() == HTTP_OPTIONS)
{
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE");
server.sendHeader("Access-Control-Allow-Headers", "*");
server.send(200);
}
else
{
handleFileRead(server.uri());
}
});
// enable CORS header in webserver results
server.enableCORS(true);
Quick explanation (i am not a pro at all):
the server.enableCORS(true); support CORS for GET method, but not for POST
When you send a POST request, the browser send a preflight with method OPTIONS and the ESP has to send a response to say what method he allows. That's the if (server.method() == HTTP_OPTIONS)
After this preflight clearing CORS, you browser send finally its POST request
It take me a lot of time to understand, so i wanted to share this solution. If you have a better one, i am happy to hear you !!!!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
Just a quick note to fix CORS error especially for testing when you are developing your client (so local file on your computer) requesting on your ESP.
chromium --disable-web-security --user-data-dir=~/test
Quick explanation (i am not a pro at all):
It take me a lot of time to understand, so i wanted to share this solution. If you have a better one, i am happy to hear you !!!!
Beta Was this translation helpful? Give feedback.
All reactions