HTML source code appears in the browser as it is

Asked 2 years ago, Updated 2 years ago, 82 views

I am creating a WebServer for ESP8266 using ESPlor.
Below is the UI code, but the code itself appears in the browser (safari, googlechrome).
Is there a problem with this code or browser?

 wifi.setmode (wifi.STATION)
wifi.sta.config ("106F3F76046E", "p518fm1rhp3na")
print("Connected on"..wifi.sta.getip()) 
wifi.sta.connect() 
-- tmr.alarm(0,1000,1,checkWifiStatus) 
gpio.mode (7, gpio.OUTPUT) 
gpio.write(7,gpio.LOW) 
if srv~=nil then
  srv —Close()
end

srv = net.createServer(net.TCP,3) 
print("Server created on"..wifi.sta.getip()) 
srv:listen(80, function(conn)conn:on("receive", function(conn, request) 
print(request) 
_,j=string.find(request, 'led_light_switch=') 
if j~=nil then command=string.sub(request,j+1) 
if command=='on'the gpio.write(7,gpio.HIGH) 
else gpio.write(7,gpio.LOW) 
end 
end 

conn:send("HTTP/1.1200 OK\r\n\r\nHello, ESP8266.")
conn:send('<!DOCTYPE html>') 
conn:send('<html lang="en">') 
conn:send('<head><metacharset="utf-8"/>') 
conn:send('<title>Hello, World!</title></head>') 
conn:send('<body><h1>Hello, World!</h1>')

if gpio.read(7) == gpio.HIGH then led = "ON" 
selected="OFF" 
end 
conn:send('<p>The light is'..led..'</p>') 
conn:send('<form method="post">') 
conn:send('<input type="radio" name="led_light_switch" value="on">ON</input><br/>') 
conn:send('<input type="radio" name="led_light_switch" value="off">OFF</input><br/>') 
conn:send('<input type="submit" value="Light Switch"/>') 
conn:send('</form></body></html>') 
end)
end)

html http

2022-09-30 16:42

1 Answers

Why don't you add Content-Type:text/html\r\n immediately after sending out HTTP/1.1200 OK\r\n?


2022-09-30 16:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.