1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

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

Discussion in 'B4J Questions' started by Mashiane, Apr 8, 2019.

  1. Mashiane

    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.

    Code:
    Sub ExecutePHPWait(pQuery As Map, phpFile As String, phpTag As StringAs 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.

    Code:
    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
     
    joulongleu likes this.
  2. Michael1968

    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:
    Code:
    BANano.UseServiceWorker=False
    Michael1968
     
    joulongleu likes this.
  3. Mashiane

    Mashiane Expert Licensed User

    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!
     
  4. Mashiane

    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....

    Code:
    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 StringAs 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
     
    joulongleu likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice