I'm trying to control some RGB LEDs with ESP8266. I have built an HTML page to set the color and send the RGB color code to the ESP using HTTP get request. Everything works as expected. I just want to send an HTTP get request to the ESP and ask what the current color is. But after a couple of hours of searching and trying I don't know how to send data in response to get request, I just can send HTTP ok code. I'm using jquery to send get command as below
$.get("http://192.168.4.1/rgb(120,50,60)", function(data,status){
//I don't know how to send data fram ESP get it here.
});
this is the corresponding ESP response code written in Arduini IDE
WiFiClient client = server.available();
if (client) {
String req = client.readStringUntil('\r');
client.print("HTTP/1.1 200 OK\r\n");
client.print("Access-Control-Allow-Origin: *");
.
.
.
This is the HTTP header, How do I send some data(text)?
1 Answer 1
@Gerben answered my question in a comment all credit goes to him. I retype it here maybe it helps someone in the future.
I had to send two new line after setting up the header
//header
client.print("\r\n\r\n");
clinet.print("Hello, I'm comming from ESP!!");
And in jQuery I get it like this:
$.get("http://192.168.4.1/rgb(120,50,60)", function(data,status){
var receivedData=data; //=Hello, I'm comming from ESP!!
});
It is simple but it may help some newbie like me.
server.send(200, "text/plain", someStringOfContentHere);
build up the string with the data you need.