Closed
Description
I am trying to connect a nodeMcu to wifi network by sending a post request with SSID and password from browser (AJAX) to Esp8266webserver. I confirmed that the server received the request with correct arguments(SSID and Password). However, occasionally the browser(chrome) debug console showed an error "net::ERR_CONNECTION_RESET". What could be the reason for that?
Javascript post request from browser:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4){
if(xhttp.status==200){
alert('Successfully connected');
}else{
alert('Unable to connect.'); // the server should return a 404
};
};
};
xhttp.open('POST', '/connect', true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send('ssid='+ssid+'&'+'pswd='+pswd);
Server Post request handler :
WiFi.begin(ssid,pswd);
while(WiFi.status() != WL_CONNECTED){
// occasionally browser got the connection reset error while serer is still running this loop.
Serial.print('.');
delay(500);
if(WiFi.status() == WL_CONNECT_FAILED) break;
}
Server.sendHeader("Connection", "close");
Server.sendHeader("Access-Control-Allow-Origin",` "*");
if(WiFi.status == WL_CONNECTED){
// connected to wifi network
Server.send(200, "text/plain", "OK");
return;
}
// unable to connect to wifi network
Server.send(404, "text/plain", "fail");
Thank you.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.