[Pas2js] TJSXMLHttpRequest questions
Mattias Gaertner
nc-gaertnma at netcologne.de
Sun May 20 12:51:44 CEST 2018
On Sun, 20 May 2018 15:59:37 +0700
Ryan Joseph <ryan at thealchemistguild.com> wrote:
> 1) How are we intended to use TJSOnReadyStateChangeHandler? I thought the callback would take a parameter to the TJSXMLHttpRequest but it doesn’t. The JavaScript way seems to be a closure so we can access the request in the same scope but we obviously don’t have those in Pascal.
There are no anonymous methods yet, but there are closures:
procedure Test;
var
xhr : TJSXMLHttpRequest;
procedure HandleLoaded;
begin
if (xhr.readyState = 4) or (xhr.status = 200) then
writeln(xhr.responseText);
end;
begin
xhr := TJSXMLHttpRequest.new;
xhr.open('GET', 'file.html');
xhr.onreadystatechange := @HandleLoaded;
xhr.send;
end;
> 2) Not really Pascal related but why does HandleLoadedFile get called 3 times? I had to call abort manually to stop it once I got the text I wanted.
See
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange
Btw, normally you use OnLoad:
xhr.addEventListener('load', @OnLoad);
Mattias
More information about the Pas2js
mailing list