hello! I have a problem, I am unable to receive a response from ESP32 when I request it via AJAX JQUERY through the website.
When I access the ESP32's IP through the browser it is responding correctly, but when making the request via AJAX the ESP32 does not respond, via serial I see that I am doing everything correctly, AJAX returns error 0
code in html
<script type="text/javascript" src="jquery.min.js"></script>
<script>
$(document).ready(function() {
alert("http://192.168.4.1/%7B%22func%22:%221%22%7D");
$('#submitOne').click(function() {
$.ajax({
url: "http://192.168.4.1/%7B%22func%22:%221%22%7D",
type: 'GET',
})
.done(function(result, textStatus, xhr) {
console.log(result);
if (xhr.status===201) $('#txtResult').text(result.id);
})
.fail(function(jqXHR, textStatus, errorThrown) {
var errorMsg = jqXHR.status + ' : ' + textStatus + errorThrown;
alert("Error - " + errorMsg);
})
})
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="latOne" value="52.373920"> <input id="lngOne" value="9.735603">
<button id="submitOne">go</button><br>
<div id="txtResult"></div>
Code ESP32
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = "";
verificaFuncao = 0;
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
if (c == '\n') {
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
Serial.println(currentLine);
if (currentLine.endsWith("HTTP/1.1")) {
Serial.println("Recebido");
currentLine.replace("%7B", "{");
currentLine.replace("%22", "\"");
currentLine.replace("%7D", "}");
currentLine.replace("GET /", "");
currentLine.replace(" HTTP/1.1", "");
Serial.println(currentLine);
DeserializationError error = deserializeJson(docRecebe, currentLine);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
verificaFuncao = docRecebe["func"];
if (verificaFuncao == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("{\"error\":\"1\"}");
client.println();
client.flush();
client.stop();
Serial.println("Client Disconnected.");
break;
} else if (verificaFuncao == 1) {
String posXPost = String(posX);
String posYPost = String(posY);
float ajustaMapa = mapaInjecao[posX][posY];
ajustaMapa = ajustaMapa / 1000;
String tempoInjecaoPost = String(ajustaMapa);
String rotacaoPost = String(rotacao);
String mapPost = String(sensorMapVolt);
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("{\"resp\":\"1\",\"posX\":\"" + posXPost + "\",\"posY\":\"" + posYPost + "\",\"rotacao\":\"" + rotacaoPost + "\",\"map\":\"" + mapPost + "\",\"tempoInjecao\":\"" + tempoInjecaoPost + "\",\"SondaLambda\":\"0\"}");
client.println();
client.flush();
client.stop();
Serial.println("Client Disconnected.");
break;
} else if (verificaFuncao == 2) {
posXRecebe = docRecebe["posX"];
posYRecebe = docRecebe["posY"];
valorInjecaoRecebe = docRecebe["valor"];
valorInjecaoRecebe = valorInjecaoRecebe * 1000;
mapaInjecao[posXRecebe][posYRecebe] = valorInjecaoRecebe;
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("{\"resp\":\"1\"");
client.println();
client.flush();
client.stop();
Serial.println("Client Disconnected.");
} else if (verificaFuncao == 3) {
String contadorPassosPost = String(contadorPassos);
String tempAguaPost = String(analogRead(pinSensorTempAgua));
String tempArPost = String(analogRead(pinSensorTempAr));
String sensorMapPost = String(analogRead(pinSensorMap));
String TPSPost = String(analogRead(pinSensorTPS));
String rotacaoPost = String(rotacao);
String sensorSonda = String(analogRead(pinSensorSonda));
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("{\"resp\":\"1\",\"passos\":\"" + contadorPassosPost + "\",\"tempAgua\":\"" + tempAguaPost + "\",\"tempAr\":\"" + tempArPost + "\",\"map\":\"" + sensorMapPost + "\",\"tps\":\"" + TPSPost + "\",\"rotacao\":\"" + rotacaoPost + "\",\"sonda\":\"" + sensorSonda + "\"}");
client.flush();
client.stop();
Serial.println("Client Disconnected.");
} else {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("{\"error\":\"1\"}");
client.println();
client.flush();
client.stop();
Serial.println("Client Disconnected.");
break;
}
}
}
}
}
image of chrome using a direct IP
image of mi code in HTML