Chat freely about anything...

User avatar
By Toshi Bass
#5622 Hi I am having some difficulty with lua code, a have the following code which I found on this board on my esp-01 to switch gpio1 off & on and it works well in postman http://192.168.0.179/gpio4=0 or /gpio4=1 however I want to incorporate a POST in a existing application on my raspberry pi, I have assumed Ajax is the way to go:

lua code on esp-01 running 0.9.4 20141222

wifi.setmode(3)
wifi.setmode(wifi.STATION)
wifi.sta.config("xxxxx","xxxxxxxx")

s=net.createServer(net.TCP)
s:listen(80,function(c)
c:on("receive",function(c,pl)
for v,i in pairs{3,4} do
gpio.mode(i,gpio.OUTPUT)
print(i,pl)
if string.find(pl,"gpio"..i.."=0") then gpio.write(i,0) end
if string.find(pl,"gpio"..i.."=1") then gpio.write(i,1) end
end
c:send("\nTMR:"..tmr.now().." MEM:"..node.heap())
c:on("sent",function(c) c:close() end)
end)
end)

My Ajax call from the raspberry pi looks like this:

$.ajax({
url: 'http://192.168.0.179',
type: 'POST',
dataType:'Type',
beforeSend: function (xhr) {xhr.setRequestHeader ("Authorization", "Basic xxxx")},
error : function() {
alert("error")},

success: function(data){
alert("data")},
})

but as expected in the chrome JavaScript console I get: XMLHttpRequest cannot load http://192.168.0.179/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.0.38:8000' is therefore not allowed access.. I have assumed I need to do a before Send Authorization request, however considering I never set eyes on lua before yesterday I would really appreciate some assistance with the luya code, thanks and Merry Christmas Toshi