Forum > Networking and Web Programming

CORS/AJAX with fpHTTPap Lazarus

(1/1)

automacaosamos:
Could you help me with an example of an API using:
fphttpapp, httpdefs, httproute, fpjson

that implements CORS to receive AJAX requests ??

thanks

Leledumbo:
fpjson not required. CORS is implemented as a HTTP OPTIONS handler, so practically just:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure Whatever(ARequest: TRequest; AResponse: TResponse);var  Origin: String;begin  if ARequest.Method = 'OPTIONS' then begin // handle CORS request    // normally, you would check the sent ORIGIN header, to see who sent the request    Origin := HttpRequest.GetCustomHeader('Origin');    // for instance, you only allow it from your own domain    if Origin = 'myowndomain.com' then begin      // send the header that allows it to request      AResponse.SetCustomHeader('Access-Control-Allow-Origin', Origin); // or if you allow it from anywhere, use '*' instead of Origin    end;  end;end; begin   HTTPRouter.RegisterRoute('/', @Whatever);end. 

Navigation

[0] Message Index

Go to full version