Lua HTTP PostAsync

时间:2016-10-24 20:19:07

标签: http lua roblox

我正在开发一个脚本,这是我到目前为止所做的:

local hs = game.HttpService
local url = "http://sugargrovetemp.comli.com/NiceAdmin/currentusers.php"
local data = {
    ["name"] = player.Name,
    ["userid"] = player.UserId
}
data = hs:JSONEncode(data)
gotback = hs:PostAsync(url, data)
data = hs:JSONDecode(data)
print("successfully ammended "..player.Name..","..player.UserId.."to the php file as string!")

我对整个PostAsync事情都很陌生,我想知道是否有人可以帮助我?

3 个答案:

答案 0 :(得分:0)

异步意味着异步,这意味着它将等待(就像你运行wait()),直到消息发送完毕后再运行其余的代码。

另外,如果那是你的问题,你只是对刚刚编码的数据使用JSONDecode而不是变量“gotback”。

答案 1 :(得分:0)

确保以服务器理解的格式发送(ApplicationUrlEncoded)

gotback = hs:PostAsync(url, data, "ApplicationUrlEncoded")

答案 2 :(得分:0)

我觉得你所拥有的是好的,但这里有一些我觉得更可靠,更轻松的事情:

-- assert(game:GetService('HttpService').HttpEnabled == true)

local function send(tab, url)
    local HttpService = game:GetService('HttpService')
    local url = (url) and (url)
        or "http://sugargrovetemp.comli.com/NiceAdmin/currentusers.php"
    local data = HttpService:JSONEncode(tab)

    data:PostAsync(url, data, "ApplicationJson")
end

现在,如果您不想实际等待回调,那么只需使用适用于协同程序的此版本:

local function send(tab, url)

    local function process(tab, url)
        local HttpService = game:GetService('HttpService')
        local url = (url) and (url)
            or "http://sugargrovetemp.comli.com/NiceAdmin/currentusers.php"
        local data = HttpService:JSONEncode(tab)

        data:PostAsync(url, data, "ApplicationJson")
    end

    local newThread = coroutine.create(function()
        process(tab, url)
    end)

    coroutine.resume(newThread)
    return newThread
end

希望它有所帮助!