Skip to content

Instantly share code, notes, and snippets.

@karlp
Last active August 29, 2015 14:10
Show Gist options
  • Save karlp/5f601b7470968a053297 to your computer and use it in GitHub Desktop.
Save karlp/5f601b7470968a053297 to your computer and use it in GitHub Desktop.
luci json post quirk
--[[
just tries to parse the json posted and reply ok/NAK
--]]
module("luci.controller.rme.demo", package.seeall)
local http = require("luci.http")
local protocol = require("luci.http.protocol")
local json = require("dkjson")
--local json = require("luci.json")
function index()
--- some valid parent...
entry({'demo'}, template("rme/remake_start"), "stupid needing parents")
entry({'demo', 'form_size2'}, call("action_form_size2"), "form_size2")
end
function action_form_size2()
local http_method = http.getenv("REQUEST_METHOD")
local rdata = {};
if http_method == "POST" then
io.stderr:write("Got post\n")
if (http.getenv("CONTENT_TYPE"):match("^application/json")) then
io.stderr:write("Got JSON\n")
local hc = http.content()
local input, rc, err = json.decode(hc)
if input then
rdata.message = "OK"
io.stderr:write("Input validated as json ok\n")
else
http.status(400, "Bad Request")
rdata.message = "NAK"
io.stderr:write("Input failed to validated: ")
-- luci.json doesn't return errors
if err then
rdata.err = err
io.stderr:write(err)
else
rdata.err = "json parsing failed, luci.json"
end
io.stderr:write("\n")
end
http.prepare_content("application/json")
http.write(json.encode(rdata))
http.close()
end
end
luci.template.render("rme/demo_size2")
end
<%+header%>
<h2>Form size demo</h2>
<input id="btn_save" class="cbi-button cbi-input-apply" type="submit" value="save"/>
<script type="text/javascript" src="/resources/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
// works
var b1 = [
{"unit_id":43,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":41,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":42,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":44,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":46,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":45,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"blah": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{"blah2": "aaaaaaaaaaaaaaa"}
];
// FAILS (the "x"s in "blah2" push it over the limit
var b2 = [
{"unit_id":43,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":41,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":42,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":44,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":46,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"unit_id":45,"type":"Water","plugin":{"short_description":"Fake plugin, responds ...","type":"user","description":"Fake plugin, responds to slaves 40-60\n","label":"massive_faker.lua (user)","name":"massive_faker.lua_user","family":"water","original_name":"massive_faker.lua","valid":true,"size":1453}},
{"blah": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{"blah2": "aaaaaaaaaaaaaaaxxxxxx"}
];
$(document).ready(function () {
$("#btn_save").click(function (item) {
console.log("Clicked to save!");
$.ajax({
url: location.href,
contentType: "application/json",
data: JSON.stringify(b2),
type: "POST"
}).done(function (data, status, jqhxr) {
console.log("Everything posted ok");
}).fail(function (jqhxr, status, err) {
console.log("something went wrong: " + err + ": " + jqhxr.responseJSON.err);
}).always(function() {
console.log("Always....");
})
})
});
</script>
<%+footer%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment