luci interacts with page data

Call lua script through XHR in htm, lua script location: /usr/lib/lua/luci/controller/admin/xxx.lua

xxx.htm:

1. Static call

var callPath='<%=luci.dispatcher.build_url("admin", "xxx", "dev_info")%>';

callPath=callPath+'/'+param1+'/'+param2; //If you need to pass parameters

 XHR.get(callPath,null,function(x,rval){

          ......

    }

);


2. Dynamic call

var callPath='<%=luci.dispatcher.build_url("admin", "xxx", "dev_info",parameter)%>';

XHR.poll(5,callPath, null,function(x,callPath rval){

        ......
    }

);

The first parameter is the timing refresh time (unit: S)

The second parameter is the path generated by luci, where parameter is the parameter passed to lua

The third parameter is temporarily unknown, filling in null does not affect

The fourth is the callback function:

           x: is an XHR object, generally not needed

           rval: is the return value of the lua function (generally the value rval[0])


3. Trigger the submit submit call

<script>

         document.getElementById("ID_INPUT_submit").value=paramt; //value value will be passed to lua
 document.getElementById("ID_INPUT_submit").click();   //触发submit事件

</script>

<form style="display:none" method="post"  action="<%=luci.dispatcher.build_url("admin", "xxx","dev_info")%>" enctype="multipart/form-data">

 <input id="ID_INPUT_submit"  name="getpackage" type="submit"  \>
 </form>

xxx.lua:

entry({"admin", "xxx", "dev_info"}, call("dev_info"), nil).leaf = true   


Same for page methods 1,2:

--paramval: Receive the passed parameters, (there are two, write two input parameters)
function dev_info(paramval)

    local function getDev(val)
              ......
              return .....
    end

    luci.http.prepare_content("application/json")
    luci.http.write('[')
    luci.http.write_json(getDev(paramval))
    luci.http.write(']')
end

Page method 3:

function dev_info()

     local value=luci.http.formvalue("getpackage") -- get the passed down value, where getpackage is the name of the input

    .......

end

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325711236&siteId=291194637