Wenn du gaanz viel Muße hast hast du ja vielleicht sogar ein funktionierendes Snippet für mich
Delphi-Quellcode:
procedure ...RequestValue(pmResultProc: TProcBoolean);
//-------- local function -------------------------------------------
function DoHandleResponse(_pmEvent: TEventListenerEvent): Boolean;
var
rspJson:
String;
rspStatus: Integer;
rspObject: TJSObject;
begin
asm
rspJson = _pmEvent.target.responseText;
rspStatus = _pmEvent.target.status;
end;
if rspStatus = 200
then
begin
rspObject := TJSJson.parseObject(rspJson);
...
end;
pmResultProc(...);
Result := True;
end;
//-------- end local function ---------------------------------------
var
req: TJSXmlHttpRequest;
requestUrl:
String;
begin
asm
var url = new
URL(this.FServiceUrl);
url.searchParams.append('
accessToken', this.FAccessToken);
requestUrl =
url.toString();
end;
req := TJSXmlHttpRequest.new;
req.addEventListener('
loadend', @DoHandleResponse);
req.open('
GET', requestUrl);
req.setRequestHeader('
X-Requested-With', '
XMLHttpRequest');
req.send;
end;
Der Request in der Funktion läuft asynchron ab. Wenn du auf den Aufruf warten musst, verwende eine Promise.
Delphi-Quellcode:
function ...LoadValue: TJSPromise;
begin
Result := TJSPromise.new(
procedure(pmSuccess, pmFailed: TJSPromiseResolver)
begin
RequestValue(
procedure(pmValue: Boolean)
begin
pmSuccess(pmValue);
end);
end);
end;
Benutzen kannst du es so:
Delphi-Quellcode:
procedure ...btnLoadValueClick(Sender: TObject);
//-------- local function -------------------------------------------
function HandleLoadValue(pmValue: JSValue): JSValue;
begin
Result := pmValue;
btnLoadValue.Enabled := True;
ShowMessage('Value available: ' + TJSString(pmValue).ToString);
end;
//-------- end local function ---------------------------------------
begin
btnLoadValue.Enabled := False;
LoadValue._then(@HandleLoadValue);
end;
Bis bald...
Thomas