簡體   English   中英

我可以使用javascript呼叫delphi xe8 REST服務帖子嗎?

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

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. 在瀏覽器上(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');

DataSnap REST Server上的CORS中

您需要做的就是在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;

請注意,原始示例使用“ *”代替域名(yourdomain.com),這將允許任何第三方訪問您的REST服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM