Your problem is that you are expecting whttp.Send
to block. Send
, as its JavaScript equivalent, is asynchronous. Before the POST
could even execute, whttp
object is freed (in the finally
block). When callback (OnReadyStateChanged
) is called, whttp
was already freed (and is now null) and you are then trying to call ReadyState
on that freed (null) object.
Another reason for confusion is that object.Free
in Delphi for Windows/OS X destroys the object while in Smart it merely sets the object
reference to nil
and leaves the destruction to JavaScript's garbage collection. That's why the whttp
is still alive after the Free
and why the OnReadyStateChanged
is called at all.
This works fine:
uses
W3System, W3Graphics, W3Components, W3Forms, W3Fonts, W3Borders, W3Application,
W3Button, W3Inet, W3Memo;
type
TForm1=class(TW3form)
procedure W3Button1Click(Sender: TObject);
private
{$I 'Form1:intf'}
whttp: TW3HttpRequest;
protected
procedure InitializeForm; override;
procedure InitializeObject; override;
procedure Resize; override;
end;
implementation
{ TForm1}
procedure TForm1.W3Button1Click(Sender: TObject);
var
wParams: string;
begin
whttp := TW3HttpRequest.Create;
whttp.OnReadyStateChange := lambda (Sender)
if (whttp.ReadyState = 4) and (wHttp.Status = 200) then
begin
W3Memo1.Text := wHttp.ResponseText;
whttp.OnReadyStateChange := nil;
whttp := nil;
end;
end;
wParams := 'cmd=TestID1';
whttp.open('POST','http://httpbin.org/post');
whttp.RequestHeaders['Content-type'] := 'application/x-www-form-urlencoded';
whttp.Send(wParams);
end;