in reply to webperl: fetching data over the web

You're right that it has to do with async execution - the fetch happens in the background, and you can't just check req.status right away - you need to register an event listener (I think you were probably just lucky in your tests that the requests returned very quickly :-) ). (Update: Oops, see replies!) See e.g. Using XMLHttpRequest.

The secondAnother issue to be aware of is that scripts in the "democode" page are run in a perl that gets ended when the main script is done; no async stuff is possible. For that, you need to either embed <script> tags in an HTML file, or use the "Run & Persist" mode of the "mini IDE". In the latter, the following works for me. (Note there are also other events on XMLHttpRequest, such as error and abort.

use warnings; use 5.028; use WebPerl qw/js js_new/; my $xhr = js_new('XMLHttpRequest'); $xhr->addEventListener("load", sub { say "Loaded! <<<<<",$xhr->{responseText},">>>>>\n"; }); $xhr->open("GET", "http://localhost:5000/"); say "Sending...\n"; $xhr->send();

Actually, I am currently working on a subclass of Future::HTTP that I'd like to include by default in WebPerl to make HTTP requests easier out of the box, hopefully I can release it soon.

Replies are listed 'Best First'.
Re^2: webperl: fetching data over the web
by LanX (Saint) on Nov 08, 2018 at 15:29 UTC
    From what I understood is my demonstrated js code synchronous and blocking. Running it directly works, also from the Dev console in FFM

    I'm not sure Perl should return earlier than alert does.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      From what I understood is my demonstrated js code synchronous and blocking.

      Sorry, I missed the third argument to your req.open() call that makes the request synchronous. But I did copy your third piece of example code exactly, and on my end it works, and only blocks the page while the fetch is running - which is to be expected, since it's a synchronous request. Not sure why it's failing for you, but like I said, check the browser's debugging tools (e.g. in Firefox, hit Ctrl+Shift+e).

      Note that synchronous XHR's aren't recommended, see e.g. here:

      Note: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), Blink 39.0, and Edge 13, synchronous requests on the main thread have been deprecated due to the negative effects to the user experience.

      Synchronous XHR often causes hangs on the web. But developers typically don't notice the problem because the hang only manifests during poor network conditions or slow server response. Synchronous XHR is now in deprecation state. Developers are recommended to move away from the API.

        I'm aware its deprecated but I only need it for a proof of concept and am suspecting the problem to be somewhere else.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice