【Lua】LWT后台用JSON与 ExtJS传递数据

编程入门 行业动态 更新时间:2024-10-11 15:23:40

【Lua】LWT<a href=https://www.elefans.com/category/jswz/34/1771386.html style=后台用JSON与 ExtJS传递数据"/>

【Lua】LWT后台用JSON与 ExtJS传递数据

  要完成目录树的构建,需要前台ExtJS构筑页面,后台处理逻辑,中间由JSON传递数据。

  首先搭建后台环境:

 1 require "httpd" 
 2 require "lfs"
 3 
 4 request, args = ... 
 5 
 6 local s = {root = { 
 7         text = "rootNode",
 8         expanded = true,
 9         children = {
10             {
11                 text = 'book1',
12                 leaf = true
13             },
14             {
15                 text = "book2",
16                 expanded = true,
17                 children = {
18                     {
19                         text = "con1",
20                         leaf = true
21                     },{
22                         text = "con2",
23                         leaf = true
24                     }
25                 }
26             },
27             {
28                 text = "book3",
29                 expanded = true,
30                 leaf = true
31             }
32         }
33     }
34 }
35 
36 local cjson = require("cjson")
37 local tJson = cjson.encode(s)
38 
39 httpd.set_content_type("text/json")
40 httpd.write(tJson)

  这里虚拟了一个树数据结构s,将其打包后返回给请求方。

  然后是html部分:

 1 <html>
 2     <head>
 3         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 4         
 5         <title>ExtJS TreePanel</title>
 6         <link rel="stylesheet" type="text/css" href="../../ext-5.0.0/examples/shared/example.css" />
 7         
 8         
 9         <script type="text/javascript" src="../ext-5.0.0/examples/shared/include-ext.js"></script>
10         <script type="text/javascript" src="../ext-5.0.0/examples/shared/options-toolbar.js"></script>
11         
12         <script type="text/javascript" src="test.js" charset="utf-8" ></script>
13   
14     </head>
15     <body style=" margin: 0; padding: 0;">
16     </body>
17 </html>

  最后是ExtJS部分:

 1 Ext.require('Ext.tree.Panel')
 2 
 3 Ext.onReady(function(){
 4     Ext.Ajax.request({
 5         url: 'm.lua',       //后台文件path
 6         method: 'post',
 7         params: {
 8             action: 'getDir',
 9         },
10         success: function(response){
11             var text = response.responseText; 
12             var obj = eval('(' + text + ')'); 
13             console.log(obj);             
14         }, 
15         failure: function() {
16             Ext.Msg.alert("失败","失败了");
17         }
18     });
19 });

  其中response内容是这样的:

  所以可以用"response.responseText"来调用响应内容的主体。

  在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键。

  JSON字符串转换为JSON对象:

    var obj = eval('(' + str + ')');

  这样就能获取一个可供使用的JSON对象了。

    

转载于:.html

更多推荐

【Lua】LWT后台用JSON与 ExtJS传递数据

本文发布于:2024-03-06 05:23:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1714476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   数据   LWT   Lua   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!