zoukankan      html  css  js  c++  java
  • 【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对象了。

        

  • 相关阅读:
    rest framework 认证 权限 频率
    rest framework 视图,路由
    rest framework 序列化
    10.3 Vue 路由系统
    10.4 Vue 父子传值
    10.2 Vue 环境安装
    10.1 ES6 的新增特性以及简单语法
    Django 跨域请求处理
    20190827 On Java8 第十四章 流式编程
    20190825 On Java8 第十三章 函数式编程
  • 原文地址:https://www.cnblogs.com/linxiong945/p/4108965.html
Copyright © 2011-2022 走看看