【Lua】LWT后台用JSON与 ExtJS传递数据
2024-10-19 19:56:26
要完成目录树的构建,需要前台ExtJS构筑页面,后台处理逻辑,中间由JSON传递数据。
首先搭建后台环境:
require "httpd"
require "lfs" request, args = ... local s = {root = {
text = "rootNode",
expanded = true,
children = {
{
text = 'book1',
leaf = true
},
{
text = "book2",
expanded = true,
children = {
{
text = "con1",
leaf = true
},{
text = "con2",
leaf = true
}
}
},
{
text = "book3",
expanded = true,
leaf = true
}
}
}
} local cjson = require("cjson")
local tJson = cjson.encode(s) httpd.set_content_type("text/json")
httpd.write(tJson)
这里虚拟了一个树数据结构s,将其打包后返回给请求方。
然后是html部分:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>ExtJS TreePanel</title>
<link rel="stylesheet" type="text/css" href="../../ext-5.0.0/examples/shared/example.css" /> <script type="text/javascript" src="../ext-5.0.0/examples/shared/include-ext.js"></script>
<script type="text/javascript" src="../ext-5.0.0/examples/shared/options-toolbar.js"></script> <script type="text/javascript" src="test.js" charset="utf-8" ></script> </head>
<body style=" margin: 0; padding: 0;">
</body>
</html>
最后是ExtJS部分:
Ext.require('Ext.tree.Panel') Ext.onReady(function(){
Ext.Ajax.request({
url: 'm.lua', //后台文件path
method: 'post',
params: {
action: 'getDir',
},
success: function(response){
var text = response.responseText;
var obj = eval('(' + text + ')');
console.log(obj);
},
failure: function() {
Ext.Msg.alert("失败","失败了");
}
});
});
其中response内容是这样的:
所以可以用"response.responseText"来调用响应内容的主体。
在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键。
JSON字符串转换为JSON对象:
var obj = eval('(' + str + ')');
这样就能获取一个可供使用的JSON对象了。
最新文章
- 【图文教程】Eclipse for PHP+XAMPP调试配置
- 【ORM】--FluentNHibernate之AutoMapping详解
- dede数据库类使用方法 $dsql
- Intellij快捷键
- AngularJS学习笔记(1)
- mysql workbench is well-designed
- [jobdu]孩子们的游戏(圆圈中最后剩下的数)
- 解决js浮点数计算bug
- 【Android UI设计与开发】第17期:滑动菜单栏(二)开源项目SlidingMenu的示例
- sql语句的使用;
- HDU - 1205 I NEED A OFFER!
- [转]html转码表
- 原生Eclipse下Java服务器调试的一个问题
- LeetCode 链表(旋转链表61)
- spring整合dubbo[单机版]
- LogFilter
- PyCharm:ModuleNotFoundError: No module named &#39;selenium&#39;
- select2插件设置选中值并显示的问题
- (转)Windows下MySQL :GUI安装和使用(MySQL GUI tools)
- Python学习---Django关于POST的请求解析源码分析
热门文章
- Backup--备份相关的信息查看及小技巧
- c# Quartz.net的简单封装
- 度度熊想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同。度度熊想买一顶价格第三便宜的帽子,问第三便宜的帽子价格是多少?
- 使用System.Data.SQLite及其EF模块操作SQLite数据库(文件)
- java 编译器级别与项目版本不匹配
- jquery之链式调用,层级菜单
- ibatis遍历数组:ParameterObject or property was not a Collection, Array or Iterator.
- 机器学习 - ML + 深度学习 - DL
- P4093 [HEOI2016/TJOI2016]序列
- Win10通电自动开机的解决办法