在浏览器上获取No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
(Crome)。
Server Delphi XE8 REST服务exe程序:
function TServerMethods.updateInsertData4(val1: string): string;
begin
result := val1;
end;
使用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');
答案 0 :(得分:1)
来自CORS on DataSnap REST Server:
您需要做的就是在之前的Response中添加自定义标头 在DataSnap服务器上调度结果
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;
请注意,原始示例使用&#39; *&#39;而不是域名(yourdomain.com),这将允许任何第三方访问您的REST服务器。