openwrt http服务: 数据提交与服务器流程

xiaoxiao2021-02-28  13

1.htm页面

jquery中的ajax方法参数总是记不住,这里有个很好的参考

function stxhr_get(datas) { var legend = document.getElementById('diag-rc-legend'); var output = document.getElementById('diag-rc-output'); if (legend && output) { output.innerHTML = '<img src="../icons/loading.gif" alt="Loading" style="vertical-align:middle" /> ' + '<font size="3" color="red">Waiting for command to complete ...</font>'; legend.parentNode.style.display = 'block'; legend.style.display = 'inline'; stxhr.get(datas, null, function(x) { if (x.responseText) { legend.style.display = 'none'; output.innerHTML = String.format('<pre><font size="3" color="blue" ><b>%h</b></font></pre>', x.responseText); } else { legend.style.display = 'none'; output.innerHTML = String.format('<pre><font size="5" color="red" style="font-weight:bold"><b>Error:Configure failed !</b></font></span>'); } } ); } } function ajax_set(data1,data2,data3,data4,data5,a_url,s_url) { $.ajax({ url:a_url, dataType:'json', method:'post', data:{value1:data1,value2:data2,value3:data3,value4:data4,value5:data5},//向服务器发送数据 success:function(r){ stxhr_get(s_url+ r.result); if(r.result == 'success'){ alert("Configuration Success ..."); setInterval("window.location.reload()",1000); } else { stxhr_get(s_url+ r.result); alert(Configuration failed ..."); } } }); } function del_black_ip_web(srcip,dstip,type,index) { ajax_set(index,type,srcip,dstip,0,'admin/config/nat_config', 'admin/config/set_infos/'); } <input type='button' name='delete_file' value='Delete' style='background-color:#f09090' οnclick='del_black_ip_web(file,file1,file2,file3,file4)'> <fieldset class="cbi-section" style="display:none">     <legend id="diag-rc-legend"><font size="3" color="red">In the configuration...</font></legend>     <span id="diag-rc-output">1</span> </fieldset>

2.lua文件

function index() entry({"admin", "config", "nat"}, template("view/nat_config"), _("NAT CONFIG"),3) entry({"admin", "config", "nat_config"}, call("nat_config"), nil).leaf = true entry({"admin", "config", "set_infos"}, call("set_infos"), nil).leaf = true end function nat_config() --解析链接中的数据 local uci = require "luci.model.uci".cursor() local value1 = luci.http.formvalue('value1') local value2 = luci.http.formvalue('value2') local value3 = luci.http.formvalue('value3') local value4 = luci.http.formvalue('value4') local action = luci.http.formvalue('value5') if '1' == action then --执行add else --执行del end local o = {} o['result'] = 'success' o['errno'] = tostring(val) o['data'] = "ok" luci.http.prepare_content("application/json") luci.http.write_json(o) --把json格式的数据串写到一个空白网页里 end function set_infos(status) luci.http.prepare_content("text/plain") if 'success' == status then luci.http.write("Success !") else luci.http.status(0,"Failed !") return end luci.http.write("\n") luci.http.status(0,"return 0") end

整个的流程是:

1. 按钮以ajax post方式把数据提交到:/admin/config/nat_config;

2. 调用lua函数net_config(),根据参数执行删除文件操作;

3. 操作的返回值以json格式写到空白页面里,作为返回值返回给post,ajax post 流程结束;

4. 调用stxhr.get,穿的参数是/admin/config/set_infos+之前json的result的值;

5. 调用lua函数set_infos(),获取返回值。至此结束

另外,csdn的文本编辑真的很让人无语啊,格式怎么总是有问题!?

转载请注明原文地址: https://www.6miu.com/read-2250103.html

最新回复(0)