I have modified the sample application TMAWeb_SimpleService return a list of json filenames from my webservice. Which has
procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.SetCustomHeader('Access-Control-Allow-Origin','*');
end;
procedure TForm1.WebButton1Click(Sender: TObject);
begin
// WebHttpRequest1.URL := 'https://download.tmssoftware.com/tmsweb/music.json';
WebHttpRequest1.URL := 'http://92.27.68.65:7777/getRockVideos';
WebHttpRequest1.Execute();
end;
procedure TForm1.WebHttpRequest1RequestResponse(Sender: TObject;
ARequest: TJSXMLHttpRequestRecord; AResponse: string);
var
js: TJSON;
ja: TJSONArray;
jo: TJSONObject;
i: integer;
begin
js := TJSON.Create;
try
ja := TJSONArray(js.Parse(AResponse));
ShowMessage('Retrieved items:' +AResponse);
for i := 0 to ja.Count - 1 do
begin
jo := TJSONObject(ja.Items[i]);
WebListBox1.Items.Add(jo.GetJSONValue('videoName'));
end;
finally
js.Free;
end;
end ;
end.
This work fine in the sample application.
When I add this code to my application I get a cross domain access error.
rocksTruck.html:1 Access to XMLHttpRequest at 'http://92.27.68.65/getRockVideos' from origin 'http://localhost:8000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I can't see any setting in the sample application that solves the issue.
Check this FAQ:
This means the REST API resource is not enabled for CORS.
Solve this by putting this JSON resource in the same domain as your web app or enable CORS on the backend. (This will depend on the type of backend you use).
For a background on CORS, please see:
For enabling CORS for XData this is explained in detail here:
I've noticed the errors. Late nights and brain drain with CSS etc etc.
My app was missing the port (7777) in my app and I was using the wrong OnRequestResponse on the second call.