B4J Question Wait until the Ajax is executed in a Webpage

iglrs1

Member
Licensed User
In a Webpage with AJAX I need to wait until the Ajax is executed. This is done after pressing a enter in an input field (Pressing the enter is done by code ). The Wait For WebView1_PageFinished(Url As String) is not working. I can't even see the alert event. Could someone help me?

B4X:
Dim js As String = $"e=jQuery.Event('keyup');
                             e.which = 13;
                             e.keyCode = 13;
                             $('input[name="test"]').keypress().trigger(e);
                             $(document).ajaxComplete(function() {
                               alert("This is an Alert!");
                             });"$

joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(js))
Wait For WebView1_PageFinished(Url As String)
 

Erel

Administrator
Staff member
Licensed User
PageFinished is not related to this script.


Adding listener for the alert event:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private WebView1 As WebView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    Dim jo As JavaObject = WebView1
    Dim event As Object = jo.CreateEventFromUI("javafx.event.EventHandler", "Alert", Null)
    jo.RunMethodJO("getEngine", Null).RunMethod("setOnAlert", Array(event))
    
    
    jo.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array($"alert("asdasd");"$))
End Sub

Sub Alert_Event (MethodName As String, Args() As Object) As Object
    If MethodName = "handle" Then
        Log(Args(0))
    End If
    Return Null
End Sub
 

iglrs1

Member
Licensed User
Thanks Erel,

Now with your code I can catch the Alert event after the ajax is completed (.ajax.Complete event), but I don’t know how to send the json data in the result variable (result = JSON.parse(msg)) to the Alert_Event function and save it in a variable to parse it.

Here what I do

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private WebView1 As WebView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    Dim jo As JavaObject = WebView1

    Dim event As Object = joWV.CreateEventFromUI("javafx.event.EventHandler", "Alert", Null)
    joWV.RunMethodJO("getEngine", Null).RunMethod("setOnAlert", Array(event))

   Dim js2 As String = $"e=jQuery.Event('keyup');
                                      e.which = 13;
                                      e.keyCode = 13;
                                      $('input[name="test"]').keypress().trigger(e);                    
                                      $(document).ajaxComplete(function() {
                                         alert(result);
                                       });"$

   joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(js2))

End sub

Sub Alert_Event (MethodName As String, Args() As Object) As Object
    If MethodName = "handle" Then
                Dim jo As JavaObject = Args(0)
                ‘How I get the json result variable from the javascript?
                Log(Args(0))
     End If

    Return Null

End sub
Here the javascript code
ajax with json data:
$.ajax({
   method: "POST",
   url: "../test.php",
   data: {
      data: filter
   }
   }).done(function(msg) {
        result = JSON.parse(msg);
});
 

iglrs1

Member
Licensed User
The Log(Args(0)) shows:
WebEvent [source = javafx.scene.web.WebEngine@3a6f302e, eventType = WEB_ALERT, data= [object Object]]

What I've done is change the Json Object to string with:

B4X:
Dim js2 As String = $"e=jQuery.Event('keyup');
                              e.which = 13;
                              e.keyCode = 13;
                              $('input[name="test"]').keypress().trigger(e);                  
                              $(document).ajaxComplete(function() {
                                  myJSON = JSON.stringify(result);
                                 alert(myJSON);               
                              });
                             "$
And now the Log(Arg(0)) shows

WebEvent [source = javafx.scene.web.WebEngine@17545ad, eventType = WEB_ALERT, data = {"employee":{"name":"sonoo","salary":56000,"married":false}}

How can I get the 'data' Json value easily? Is it possible to convert the answer to an object?
 
Last edited:
Top