B4J Question [BANAno] [SOLVED] CallAjaxWait for dummies...

Mashiane

Expert
Licensed User
Hi there

I kindly need some help. I have written a sub that's supposed to return the string value from CallAjaxWait as depicted below. I am calling an server php file.

B4X:
Sub ExecutePHPWait(pQuery As Map, phpFile As String, phpTag As String) As String
    Dim json As String
    Dim sCommand As String = ""
    json = Map2QueryString(pQuery)
    If Len(json) = 0 Then
        sCommand = $"${PhpPath}${phpFile}"$
    Else
        sCommand = $"${PhpPath}${phpFile}?${json}"$
    End If
    'create the headers
    Dim headers As Map
    headers.Initialize
    headers.put("Content-Type", "application/json")
    headers.Put("Access-Control-Allow-Origin", "*")
    Dim result As String = banano.CallAjaxWait(sCommand, "GET", "json","",False, headers)
    Return result
End Sub
I am calling this from my button click event, and this when logged shows a Promise on the console. One scenario is either I get to process further things on it using a BANano.Promise object as I need to process the results.

B4X:
Sub loginw(e As BANanoEvent)
    Dim strUser As String = banano.GetElement("#txtusername").GetValue
    Dim strPwd As String = banano.GetElement("#txtpassword").GetValue
    'build php query
    Dim straction As String = "validateuser"
    Dim dbAction As Map
    dbAction.Initialize
    dbAction.Put("action=", straction)
    dbAction.Put("username=", strUser)
    dbAction.Put("userpassword=", strPwd)
    'execute php
    Dim res As String = ExecutePHPWait(dbAction,"users.php",straction)
    Log(res)
    Log("Done")
End Sub
Is there a way to pass the res variable to a promise object so that I can get the result of the php result or some other way to access the records returned that are stored in the "value" of the returned promise?

Thanks in advance
 

Michael1968

Active Member
Licensed User
Hi Mashine,

I have a probleme with BANano.CallInlinePHPWait ,it shows me a Promise on the console too.

my probleme solved with:
B4X:
BANano.UseServiceWorker=False
Michael1968
 

Mashiane

Expert
Licensed User
BANano.CallInlinePHPWait
Hi, thanks Michael1968, that was worth a short. I am not calling inline php but an external php file and already the UserServiceWorker = False.

I want to extend my code here with an option for the BANano.CallAjaxWait, at the moment I'm so clueless as to how this whole thing works.

Ta!
 

Mashiane

Expert
Licensed User
@Mashiane : Silly me! ALWAYS REMEMBER that BANANo.CallAjaxWait CANNOT return a value and SHOULD NOT be used as return value for a sub!

SOLUTION: As the code will be called a mutliple of times for different PHP files....

B4X:
Sub loginw(e As BANanoEvent)
    Dim strUser As String = banano.GetElement("#txtusername").GetValue
    Dim strPwd As String = banano.GetElement("#txtpassword").GetValue
    'build php query
    Dim straction As String = "validateuser"
    Dim dbAction As Map
    dbAction.Initialize
    dbAction.Put("action=", straction)
    dbAction.Put("username=", strUser)
    dbAction.Put("userpassword=", strPwd)
    'build the php
    Dim m As Map = BuildPHP(dbAction,"users.php",straction)
    Dim headers As Map = m.Get("headers")
    Dim sCommand As String = m.Get("command")
    Dim result As String = banano.CallAjaxWait(sCommand, "GET", "json","",False, headers)
    Log(result)
    Log("Done")
End Sub

'build the php to execute
Sub BuildPHP(pQuery As Map, phpFile As String, phpTag As String) As Map
    Dim json As String
    Dim sCommand As String = ""
    json = Map2QueryString(pQuery)
    If Len(json) = 0 Then
        sCommand = $"${PhpPath}${phpFile}"$
    Else
        sCommand = $"${PhpPath}${phpFile}?${json}"$
    End If
    'create the headers
    Dim headers As Map
    headers.Initialize
    headers.put("Content-Type", "application/json")
    headers.Put("Access-Control-Allow-Origin", "*")
    'create map things
    Dim m As Map = CreateMap("headers":headers,"command":sCommand)
    Return m
End Sub
 
Top