Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
News

CORS on DataSnap REST Server

Cross-origin resource sharing (CORS) is a mechanism that enables resources to be shared across domains. Typically this isn’t allowed to prevent security issues. To enable on your DataSnap REST server you can use the following code per MVP Nirav Kaku from India.

All you need to do is add a custom header in the Response before dispatching the result on the DataSnap server…

procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin // allows cross domain calls
  Response.SetCustomHeader('Access-Control-Allow-Origin', '*');
  if Assigned(FServerFunctionInvokerAction) then
    FServerFunctionInvokerAction.Enabled := AllowServerFunctionInvoker;
end;

It is useful for DataSnap server developers who want their REST calls to be supported via AJAX using JavaScript from a different server.

[Reference]

Note: CORS is security feature of the browser so there could be some dependency there. Tested with Firefox, Chrome and IE and it seems to be working fine.

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Director of Delphi Consulting for GDK Software USA. Many software related patents, including swipe and pattern unlock and search engines. First Silver and Gold Delphi badges on Stack Overflow Former Developer Advocate for Embarcadero Technologies. Long time fan of programming, especially with Delphi. Author, Podcaster/YouTuber, Improvisor, Public Speaker, Father, and Friend.

2 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES