Returning Data to Xojo Web App

Well, just off the top of my head, you could use the jQuery AJAX call to send a request to the app and handle that in HandleURL. Just for a simple test…
I added a constant called kSendData to the default page with the following content:

var dataObject = {
    session:"#sessionID#",
    values:"#values#"
  }

$.ajax({
  method: "POST",
  url: "/dataReturn",
  data: JSON.stringify(dataObject)
});

I then added a button to the default page with this as its Pressed event:

Sub Pressed() Handles Pressed
  var r as new Random
  self.ExecuteJavaScript( kSendData.ReplaceAll( "#sessionID#", Session.Identifier ).ReplaceAll( "#values#", r.InRange( 100, 200 ).ToString ) )
End Sub

Added a property to the Session object, which I then made computed and added a break in the Setter:

Public Property currentValue as Integer

And, finally, in App.HandleURL:

if Request.path = "dataReturn" then
  var jsonResponse as new JSONItem( Request.Body )
  if jsonResponse.HasName( "session" ) and jsonResponse.HasName( "values" ) then
    var theSession as WebSession = app.SessionWithIdentifier( jsonResponse.Value( "session" ).StringValue )
    if theSession <> nil then
      Session( theSession ).currentValue = jsonResponse.Value( "values" ).IntegerValue
      Response.Status = 200
    end if
  end if
end if

Running this project, when I click on the button the data is sent to the App via an HTTP request. In the HandleURL event I check for the path, validate the data, find the Session, and set the currentValue property’s value.

There are a number of ways this could be done, that’s just one I cooked up in about five minutes.

1 Like

must have been a pressure cooker.

Thanks for the effort. You’re working in the 2020 version. I’m still in the 2019 version due to many things that still need to be added in 2020.

There is no Request.Body in 2019 so I tried to adjust it to no avail (used request.entity instead). No matter what I did Xojo would not accept Response.Status so I commented that line out to see if I could get lucky.

I clicked on the button and got the following javasript error:

This application has encountered an error and cannot continue.

Could not execute returned javascript: $ is not defined
Source: var dataObject = {
session:“7DC35572D0BB28A81B5594FF9CCA9FFFD4F42FF5”,
values:“176”
}

$.ajax({
method: “POST”,
url: “/dataReturn”,
data: JSON.stringify(dataObject)
});

Any ideas?

If you’re still on Web 1.0, then there are examples for accomplishing this sort of task that ship with the IDE. The reason you get that error is because jQuery is not loaded by Xojo in Web 1.0.

That’s the problem there are not examples, not one example of transferring data to xojo.

Since I’m trying to do a drag and drop between weblistboxes, until they give mouse events again none of this really matters

Thanks anyway.

I don’t understand. Web 2.0 is where Xojo is now missing mouse events. Either way, I’ll take a look around and work up something to help you.

OK, modified for Web 1.0.

kSendData:

var dataObject = {
    session:"#sessionID#",
    values:"#values#"
  }

var xhr = new XMLHttpRequest();
xhr.open("POST", '/dataReturn', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(JSON.stringify(dataObject));

Button1.Action:

dim r as new Random
self.ExecuteJavaScript( kSendData.ReplaceAll( "#sessionID#", Session.Identifier ).ReplaceAll( "#values#", r.InRange( 100, 200 ).ToString ) )

App.HandleURL:

if Request.path = "dataReturn" then
  dim jsonResponse as new JSONItem( Request.Entity )
  if jsonResponse.HasName( "session" ) and jsonResponse.HasName( "values" ) then
    dim theSession as WebSession = app.SessionWithIdentifier( jsonResponse.Value( "session" ).StringValue )
    if theSession <> nil then
      Session( theSession ).currentValue = jsonResponse.Value( "values" ).IntegerValue
      Return True
    end if
  end if
end if

That’s why I’m not using the Web 2.0 . . . Drag and drop will not work in 2020 version

Ok, I added a button to give me a msgbox with the number. It worked.

Now let’s see if I can put this into my app. Just remember that I’m a moderately talented amateur, not a professional.

Thanks very much

Best of luck! I have faith in you. :slight_smile:

I imagine you’ll have to use this with SSL or an app load balancer to scale your Web app, otherwise the callback from the browser may not go to the correct Web app instance with round-robin load balancing for example.

This is the only documentation for how to use triggerServerEvent in Web 2.0. Is there a way we can make this / keep this more visible?

Also, @Greg_O_Lone, there’s no information on what this means or how one might “register an event”. How would one pass “false” and get it to work?

All web controls “know” which events have been implemented, whether in the IDE or through AddHandler. That list is communicated to the browser framework and when you call TriggerServerEvent, the framework checks before sending. Setting that parameter to True overrides that behavior. It’s especially good in situations where the user would have an expectation that the value would be on the server anyway. WebListBox.SelectedRowIndex is a good example. Regardless of whether SelectionChanged is implemented, the event is always sent to the server so the value will always be there.

Event “registration” is simply “making an event definition.”

1 Like

Docs are coming. I’ve been off this week, but am hoping to have them at least in a 1st draft within the next two weeks.

1 Like

Also keen to know this. Assume I could use the app instance indicator I pass such that HAProxy connects with the right instance from a Sesdion management perspective to satisfy thi s need also ?

Now I’m stuck on the asynchronous problem . . . My app is now getting and putting my data in a variable but using the data isn’t possible. Version 2019r3.2.

This is typically done as a session cookie that’s set by the proxy engine itself so the app doesn’t need to know anything about how things are routed.

Ok, so as part of the ongoing WebSDK documentation project, I thought I’d share the example project of how to do a simple callback object so you can call a javascript method on the browser which raises an event in the app. You can get it here.

5 Likes

Nice example, straight to the point and no mixing up with other subjects!

That’s the way, the way i like it!

Thanks, Andre