fetch跨域调用datasnap服务时options命令的处理

procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  if UpperCase(Request.Method) = 'OPTIONS' then
  begin
    Response.SetCustomHeader('Access-Control-Allow-Origin','*');
    Response.SetCustomHeader('Access-Control-Allow-Credentials','true');
    Response.SetCustomHeader('Access-Control-Allow-Headers','*');
    Response.SetCustomHeader('Access-Control-Allow-Methods', '*');
    Handled := True;
  end;

  if FServerFunctionInvokerAction <> nil then
    FServerFunctionInvokerAction.Enabled := AllowServerFunctionInvoker;
end;

fetch在设置了headers后,发送get方法前会先发送options去检测。 而delphi XE10.2 创建的datasnap服务程序,WebModule上的DSHTTPWebDispatcher无法处理http的options命令,导致发生异常,解决办法如上。

原文地址:https://www.cnblogs.com/Jiaojiawang/p/13939413.html