B4J Tutorial [BANano] Ajax calls

Discussion in 'B4J Tutorials' started by alwaysbusy, Dec 3, 2018.

  1. alwaysbusy

    alwaysbusy Expert Licensed User

    EDIT 1.04: Method syntaxes have changed!

    With BANano, it is easy to make a Ajax call (e.g. to call a B4J REST API method).

    There are two ways to do so:

    1. Using events (the prefered way, as it keeps your app responsive if it is a long call)

    Code:
    ...
        BANano.CheckInternetConnection(
    "ForAjaxCallFromButton"
    End Sub

    Sub BANAno_IsConnectedResult(Tag As String, Result As Boolean)
       
    Log("Internet for: " & Tag & " = " & Result)
       
    Select Case Tag
           
    Case "ForAjaxCallFromButton"
               
    If Result Then               
                   
    Dim headers As Map
                   headers.Initialize
                   headers.put(
    "Content-Type""application/json")
                   BANano.CallAjax(
    "https://reqres.in/api/users?page=1","GET","jsonp""","ID0001"False, headers)
               
    Else
                   MiniCSS.Content(
    "#r4c1""contajax""Cannot make Ajax call because you are not connected to the internet, but here is the last result:<br>" & Shared.Ajaxresult)
               
    End If
       
    End Select
    End Sub

    Sub BANano_CallAjaxResult(Success As Boolean, UniqueID As String, Result As String
       Shared.Ajaxresult = Result
       MiniCSS.Content(
    "#r4c1""contajax", Result)
    End Sub
    2. Wait (from 1.02+)

    Does the same as the above:
    Code:
    If BANano.CheckInternetConnectionWait Then       
           
    Dim headers As Map
           headers.Initialize
           headers.put(
    "Content-Type""application/json")
           
    Dim res As String = BANano.CallAjaxWait("https://reqres.in/api/users?page=1","GET","jsonp"""False, headers)
           Shared.Ajaxresult = res
           MiniCSS.Content(
    "#r4c1""contajax", res)
       
    Else
           MiniCSS.Content(
    "#r4c1""contajax""Cannot make Ajax call because you are not connected to the internet, but here is the last result:<br>" & Shared.Ajaxresult)
       
    End If
    Alain
     
    Last edited: Dec 6, 2018
  2. alwaysbusy

    alwaysbusy Expert Licensed User

    EDIT 1.04: Method syntaxes have changed!
     
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