Last active
February 9, 2016 10:48
-
-
Save karlp/e68783fa9f50f6a49e3f to your computer and use it in GitHub Desktop.
jquery ajax post application/json to luci openwrt lua web admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module("luci.controller.rme.jquery", package.seeall) | |
local http = require("luci.http") | |
local protocol = require("luci.http.protocol") | |
local json = require("luci.json") | |
function index() | |
entry({'karl', 'modbus_devices'}, call("action_modbus_devices"), "Modbus Devices", 20).dependent=false | |
end | |
function kkdumpt(tt, label) | |
if tt then | |
if label then io.stderr:write(string.format("dumping table: %s\n", label)) end | |
for k,v in pairs(tt) do | |
io.stderr:write(string.format("k=%s\n", tostring(k))) | |
end | |
else | |
io.stderr:write(string.format("table was nil: %s\n", label)) | |
end | |
end | |
function action_modbus_devices() | |
local http_method = http.getenv("REQUEST_METHOD") | |
kkdumpt(http.getenv(), "http.getenv") | |
if http_method == "POST" then | |
local hc = http.content() | |
io.stderr:write(string.format("karl early post content = %s\n", tostring(hc))) | |
io.stderr:write(string.format("posted content type = %s\n", tostring(http.getenv("CONTENT_TYPE")))) | |
if (http.getenv("CONTENT_TYPE") == "application/json") then | |
io.stderr:write("karl got posted app/json as expected\n") | |
-- TODO - do something useful with "hc"" | |
--modbus_devices_save(hc) | |
local rdata = { message = "it sort of maybe worked..."} | |
http.prepare_content("application/json") | |
http.write(json.encode(rdata)) | |
http.close() | |
return | |
end | |
end | |
-- fall through if invalid post | |
luci.template.render("rme/karl") | |
return | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- errors here are from luci prior to r10531 | |
-- current versions of luci in trunk and AA work as expected. | |
dumping table: http.getenv | |
k=SERVER_NAME | |
k=SCRIPT_NAME | |
k=QUERY_STRING | |
k=HTTP_ACCEPT_ENCODING | |
k=SERVER_ADDR | |
k=GATEWAY_INTERFACE | |
k=REMOTE_ADDR | |
k=CONTENT_LENGTH | |
k=SERVER_PORT | |
k=SCRIPT_FILENAME | |
k=REQUEST_URI | |
k=SERVER_PROTOCOL | |
k=REMOTE_HOST | |
k=HTTP_HOST | |
k=HTTP_ACCEPT_LANGUAGE | |
k=HTTP_REFERER | |
k=REDIRECT_STATUS | |
k=PATH | |
k=CONTENT_TYPE | |
k=SERVER_SOFTWARE | |
k=HTTP_COOKIE | |
k=DOCUMENT_ROOT | |
k=HTTP_ACCEPT | |
k=HTTP_CONNECTION | |
k=PATH_INFO | |
k=HTTP_USER_AGENT | |
k=REMOTE_PORT | |
k=REQUEST_METHOD | |
karl early post content = nil <<<<===== :( | |
posted content type = application/json | |
karl got posted app/json as expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%+header%> | |
<h2><%:Modbus Devices%></h2> | |
<input id="klicky" class="cbi-button cbi-input-apply" type="submit" data-bind="click: save" value="save"/> | |
<script type="text/javascript" src="/resources/jquery-2.1.1.min.js"></script> | |
<script type="text/javascript"> | |
function clicky() { | |
console.log("saving...."); | |
var dataToSave = { | |
something: "asdfasd asdfa asdf", | |
klist: [ "item1", "item2"], | |
knum: 456 | |
}; | |
//alert("Could now send this to server: " + JSON.stringify(dataToSave)); | |
var pdata = JSON.stringify(dataToSave); | |
console.log("posting: " + pdata); | |
$.ajax({ | |
url: location.href, | |
contentType: "application/json", | |
data: pdata, | |
type: "POST" | |
}) | |
} | |
$(function() { | |
$("#klicky").click(clicky); | |
}); | |
</script> | |
<%+footer%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment