2016-10-24 43 views
0

我正在開發一個腳本,這是我到目前爲止有:Lua的HTTP PostAsync

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的事情,我想知道如果有人能幫助我嗎?

+2

你需要解釋什麼_doesn't_任何人都無法幫助你。 – Oka

回答

0

異步意味着異步,這意味着它基本上會等待(就像運行wait()),直到在運行其餘代碼之前發送消息。

此外,您剛剛對剛剛編碼的數據使用了JSONDecode,而不是變量「gotback」,如果這是您的問題。

0

確保您在您的服務器理解的格式(ApplicationUrlEncoded)

gotback = hs:PostAsync(url, data, "ApplicationUrlEncoded") 
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 

希望它有幫助!