Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread TServerRequest Quandary
Fri, Feb 9 2024 6:41 PMPermanent Link

Terry Swiers

Hi Everyone,

Just starting with EWB and I'm trying to figure out a conditional lack of response from a rest server in EWB.   I have a simple EWB app that has a button, a TServerRequest, and a TLabel on it.  The relevant code for the for is as follows:

  TForm1 = class(TForm)
     Label1: TLabel;
     Button1: TButton;
     ServerRequest: TServerRequest;
     procedure Button1Click(Sender: TObject);
     procedure ServerRequestComplete(Request: TServerRequest);
  private
     { Private declarations }
  public
     { Public declarations }
  end;

var
  Form1: TForm1;

implementation

procedure TForm1.Button1Click(Sender: TObject);
begin
ServerRequest.URL := 'https://ewbtest.atrex3.com/index.php/wp-json/wc/v3/';
ServerRequest.Execute;
end;

procedure TForm1.ServerRequestComplete(Request: TServerRequest);
begin
Label1.Caption := IntToStr(Length(Request.ResponseContent.Text));
end;

end.

It's pretty simple.  Click the button, set the URL, set the label caption to the length of the response.  

Here is the quandary.  If I open up the URL property for the server request, paste the URL there and click the test button, I get the expected response.   If I build and run the app either directly from the file path or via IIS on a local domain, I get the expected response.   But if I compile and run the app directly from within EWB, I get no content.

Is this behavior expected?
Sat, Feb 10 2024 9:23 PMPermanent Link

erickengelke

Avatar

Terry Swiers wrote:

> Here is the quandary.  If I open up the URL property for the server request, paste the URL there and click
> the test button, I get the expected response.   If I build and run the app either directly from the file path or
> via IIS on a local domain, I get the expected response.   But if I compile and run the app directly from within
> EWB, I get no content.

You may be running into CORS.   

Open your favorite web browser, point to your local machine eg. http://localhost/whatever_app_name.html
open the debugger and press Reload.  You will likely see CORS error in red on the debug console if you are running into CORS, or it may say Cross Origin Request error, or something similar.

Basically, most web servers are configured to NOT allow pages on one server (like your local machine) point to web resources on another.   You can disable CORS and then this problem goes away, but it's less secure.

I'll let you look up CORS yourself on the web for more details.

Good luck.
Erick
EWB Programming Books and Nice Component Library
See my EWB BLOG posts, at:
http://www.erickengelke.com
Sat, Feb 10 2024 9:23 PMPermanent Link

erickengelke

Avatar

Terry Swiers wrote:

> Here is the quandary.  If I open up the URL property for the server request, paste the URL there and click
> the test button, I get the expected response.   If I build and run the app either directly from the file path or
> via IIS on a local domain, I get the expected response.   But if I compile and run the app directly from within
> EWB, I get no content.

You may be running into CORS.   

Open your favorite web browser, point to your local machine eg. http://localhost/whatever_app_name.html
open the debugger and press Reload.  You will likely see CORS error in red on the debug console if you are running into CORS, or it may say Cross Origin Request error, or something similar.

Basically, most web servers are configured to NOT allow pages on one server (like your local machine) point to web resources on another.   You can disable CORS and then this problem goes away, but it's less secure.

I'll let you look up CORS yourself on the web for more details.

Good luck.
Erick
EWB Programming Books and Nice Component Library
See my EWB BLOG posts, at:
http://www.erickengelke.com
Sun, Feb 11 2024 1:41 PMPermanent Link

Terry Swiers

Hi Erick,

> You may be running into CORS.   

> Open your favorite web browser, point to your local machine eg. http://localhost/whatever_app_name.html
open the debugger and press Reload.  You will likely see CORS error in red on the debug console if you are running into CORS, or it may say Cross Origin Request error, or something similar.

I did my research here before posting and enabled cross origin to all sites.  And if I open up the project in a local browser directly ( http://localhost:88/project1.html ), it works as expected.  It just doesn't work from within EWB.

Thanks for the response.

Terry
Sun, Feb 11 2024 7:17 PMPermanent Link

Bruno Larochelle

Hi Terry,

I get around this CORS stuff by using PHP scripts that make the requests to the external server(s). I keep these PHP scripts in a '/scripts' sub-folder where my EWB app is deployed.

Below is an example of PHP script, called 'gettaf.php', that calls a National Weather Service (USA) server script and retrieves an aviation forecast. This is the script that my EWB app calls (using TServerRequest), and it works like a charm.

Hope that helps!

.. Bruno

<?php

//2022-05-18 BBL
//This script calls the NWS ADDS and retrieves the latest TAF for the specified station
//The single parameter sent to the script is the 4-leter station identifier
//Example: gettaf.php&station=CYEG

$station = $_GET['station'];

//NEW ADS SERVER... >= 2023-10-17
//FOR FULL API... SEE: https://aviationweather.gov/data/api/
$data = file_get_contents("https://aviationweather.gov/cgi-bin/data/taf.php?format=xml&ids=" . $station);

echo $data;

?>



Terry Swiers wrote:

Hi Everyone,

Just starting with EWB and I'm trying to figure out a conditional lack of response from a rest server in EWB.   I have a simple EWB app that has a button, a TServerRequest, and a TLabel on it.  The relevant code for the for is as follows:

  TForm1 = class(TForm)
     Label1: TLabel;
     Button1: TButton;
     ServerRequest: TServerRequest;
     procedure Button1Click(Sender: TObject);
     procedure ServerRequestComplete(Request: TServerRequest);
  private
     { Private declarations }
  public
     { Public declarations }
  end;

var
  Form1: TForm1;

implementation

procedure TForm1.Button1Click(Sender: TObject);
begin
ServerRequest.URL := 'https://ewbtest.atrex3.com/index.php/wp-json/wc/v3/';
ServerRequest.Execute;
end;

procedure TForm1.ServerRequestComplete(Request: TServerRequest);
begin
Label1.Caption := IntToStr(Length(Request.ResponseContent.Text));
end;

end.

It's pretty simple.  Click the button, set the URL, set the label caption to the length of the response.  

Here is the quandary.  If I open up the URL property for the server request, paste the URL there and click the test button, I get the expected response.   If I build and run the app either directly from the file path or via IIS on a local domain, I get the expected response.   But if I compile and run the app directly from within EWB, I get no content.

Is this behavior expected?
Mon, Feb 12 2024 8:00 AMPermanent Link

erickengelke

Avatar

Terry Swiers wrote:

Hi Erick,

>> You may be running into CORS.   

> I did my research here before posting and enabled cross origin to all sites.  And if I open up the project
> in a local browser directly ( http://localhost:88/project1.html ), it works as expected.  It just doesn't work
> from within EWB.

So, EWB currently uses the Internet Explorer widget provided by Windows.  It is among the worst, least standards-compliant browsers out there.  I wouldn't spend much time worrying about it.  Microsoft heavily discourages it for ages and I ran into many things it plain does wrong.  But Tim probably used it because it was the only browser (at the time) guaranteed to be on every computer.

I would recommend using Chrome, Chromium or Firefox or similar to test your code - that is the production market.  

You just keep the browser open on your desktop and use it (hit refresh) when you want to view the web page after you press the compile button.   It has advantages like the Debug window which shows you network traffic and can be used to debug the resultant Javascript if there is a problem.  

Erick
EWB Programming Books and Nice Component Library
See my EWB BLOG posts, at:
http://www.erickengelke.com
Thu, Feb 15 2024 2:48 PMPermanent Link

Terry Swiers

Thank you for the suggestions Erick,  it is greatly appreciated.
Image