Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
Networking and Web Programming
»
CORS/AJAX with fpHTTPap Lazarus
Free Pascal
Website
Downloads
Wiki
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
MOVED: Frame reception pr...
by
trev
[
Today
at 01:23:12 am]
MOVED: Regex conversion e...
by
trev
[
Today
at 01:20:23 am]
MOVED: Raylib4.0 don't wo...
by
trev
[
Today
at 01:19:51 am]
installation warning
by
trev
[
Today
at 12:12:22 am]
Raylib4.0 don't work eith...
by
dsiders
[May 22, 2022, 11:51:22 pm]
Frame reception problem i...
by
avra
[May 22, 2022, 11:32:45 pm]
HTML to text
by
wp
[May 22, 2022, 11:00:03 pm]
Lazarus Release 2.2.2
by
folkeu08
[May 22, 2022, 09:31:08 pm]
Vector graphics floodfill...
by
BobDog
[May 22, 2022, 08:51:45 pm]
Regex conversion escapes
by
AlexTP
[May 22, 2022, 08:19:24 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: CORS/AJAX with fpHTTPap Lazarus (Read 3897 times)
automacaosamos
New Member
Posts: 11
CORS/AJAX with fpHTTPap Lazarus
«
on:
November 22, 2021, 04:27:45 pm »
Could you help me with an example of an API using:
fphttpapp, httpdefs, httproute, fpjson
that implements CORS to receive AJAX requests ??
thanks
Logged
Leledumbo
Hero Member
Posts: 8416
Programming + Glam Metal + Tae Kwon Do = Me
Re: CORS/AJAX with fpHTTPap Lazarus
«
Reply #1 on:
December 07, 2021, 05:49:41 am »
fpjson not required. CORS is implemented as a HTTP OPTIONS handler, so practically just:
Code: Pascal
[Select]
[+]
[-]
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
.
Logged
Follow this if you want me to answer:
http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F
http://pascalgeek.blogspot.com
https://bitbucket.org/leledumbo
https://github.com/leledumbo
Code first, think later - Natural programmer B)
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
Networking and Web Programming
»
CORS/AJAX with fpHTTPap Lazarus
TinyPortal
© 2005-2018