简体   繁体   English

我可以使用javascript呼叫delphi xe8 REST服务帖子吗?

[英]Can I use javascript to call delphi xe8 REST service post?

Getting No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.获取No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. on browser (Crome). 在浏览器上(Crome)。

Server Delphi XE8 REST service exe program: Server Delphi XE8 REST服务exe程序:

function TServerMethods.updateInsertData4(val1: string): string;
begin
  result := val1;
end;

Javascript: 使用Javascript:

xmlhttp.open("POST", "http://..hods/InsertData4", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.json = false;
xmlhttp.timeout = 1000 * 10;
xmlhttp.ontimeout = function () { alert("Timed out!!!"); }
xmlhttp.onerror = function () { alert("error"); }
xmlhttp.send('data');

From CORS on DataSnap REST Server : DataSnap REST Server上的CORS中

All you need to do is add a custom header in the Response before dispatching the result on the DataSnap server 您需要做的就是在DataSnap服务器上分派结果之前,在Response中添加一个自定义标头

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

Note that the original example used '*' instead of a domain name (yourdomain.com), which would allow any third party to access your REST server. 请注意,原始示例使用“ *”代替域名(yourdomain.com),这将允许任何第三方访问您的REST服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM