在Lua中提交POST数据

我现在正在学习 Lua。我需要能够访问 post 和 get 数据。我正在尝试找出 Lua 中相当于 PHP $_POST 和 $_GET 的内容。

原文链接 https://stackoverflow.com/questions/4917345

点赞
stackoverflow用户108741
stackoverflow用户108741

由于 Lua 并非设计为 Web 脚本语言,因此没有等效的方案。你是在何种环境下使用它(CGI、FCGI、Apache 模块)?你可能需要研究 CGI 规范并从 Lua 访问环境变量和 stdin。

There is no equivalent as Lua is not designed as a web scripting language. In what context are you using this (CGI, FCGI, Apache module)? You'll probably need to look into the CGI specification and accessing environment variables and stdin from Lua.
2011-02-07 00:58:55
stackoverflow用户499395
stackoverflow用户499395

这取决于你所使用的网络服务器和任何中间库。

在 Apache 2.3 中,使用内置的 mod_lua,可以如下所示:

function my_handler(r)
    -- 解析 URI 参数
    local simple, full = r:parseargs()

    -- 解析 POST 请求体
    local simple, full = r:parsebody()
end

其中,simple 是一个键值对的表(大多数情况下你需要它),而 full 是一个键 -> [value1, value2, ...] 的表,用于处理重名参数的情况。

你可以在这里找到更详细的示例。

2011-02-07 03:36:07
stackoverflow用户6236
stackoverflow用户6236

Lua有许多网络框架,每种框架都有自己的访问GETPOST的方法。

学习用于Web开发的Lua最简单的方法可能是使用WSAPI

要获取GETPOST,请在处理程序中使用wsapi.request

require 'wsapi.request'

local handler = function(env)
  local request = wsapi.request.new(env)
  local GET = wsapi.request.GET
  local POST = wsapi.request.POST

  ...
end
2011-02-07 13:23:40
stackoverflow用户860130
stackoverflow用户860130

你可以随时查看 Lua4Web https://github.com/schme16/Lua4Web

2011-07-25 11:22:01
stackoverflow用户2108644
stackoverflow用户2108644

在 Lua 中,以传统的 HTML 表单或 URL 编码格式读取 POST 数据是一件混乱的事情。最好尝试使用 AJAX 表单 JavaScript 库,这样您就可以将数据以 JSON 的形式发送回服务器,然后可以轻松地解析和使用。

2014-07-28 02:48:12