Mobile games are currently dominated by weak networking, while http logon is a common function. We will implement it now.
Automatically requests http. function MainScene: ctor () at startup ()
Local url = "http: // localhost/request. php"
Local request = network. createHTTPRequest (onRequestFinished, url, "POST ")
Request: addPOSTValue ("KEY", "VALUE ")
-- Start the request. The callback () function is called when the request is complete.
Request: start ()
End
--- Do not add MainScence before onRequestFinished.
Function onRequestFinished (event)
Local OK = (event. name = "completed ")
Local request = event. request
If not OK then
-- Request failed, error code and error message displayed
Print (request: getErrorCode (), request: getErrorMessage ())
Return
End
Local code = request: getResponseStatusCode ()
If code ~ = 200 then
-- The request ends, but no 200 response code is returned
Print (code)
Return
End
-- The request is successful and the content returned by the server is displayed.
Local response = request: getResponseString ()
Print (response)
Endrequest. php implementation code:
$ TmpName = $ _ POST ['key'];
Echo $ tmpName. "OK ";
?>
Normal network output:
VALUEok, then we can add the account and password function (request: addPOSTValue ("KEY", "VALUE") on the mobile app, corresponding to the name and VALUE), and then in php, add the corresponding database query function and return different results.