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.
In reply to Re: webperl: fetching data over the web
by haukex
in thread webperl: fetching data over the web
by LanX
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |