1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| procedure TForm1.FormCreate(Sender: TObject);
begin
IdHTTPServer1.DefaultPort := 8888;
IdHTTPServer1.Active := TRUE;
end;
procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
//Renvoie la page complète s'il n'y a pas de paramètre
if ARequestInfo.Params.Count = 0 then
begin
AResponseInfo.ContentText := '<html>'
+'<head>'
+'<title>Mon test TIdHttpServer</title>'
+'</head>'
+'<body>'
+'<script>'
+'function DoSayHello() {'
+'var HttpRequest = new XMLHttpRequest();'
+'HttpRequest.open("GET", "/?sayhello", false);'
+'HttpRequest.send(null);'
+'document.getElementById(''hello_id'').innerHTML += HttpRequest.responseText +"<br>";'
+'}'
+'</script>'
+'<input type="button" value="Say Hello" onclick="DoSayHello();" />'
+'<p id="hello_id"></p>'
+'</body>'
+'</html>';
end
//Retourne "Hello world !" si le paramètre est correct (AJAX)
else if SameText(ARequestInfo.Params[0], 'sayhello') then
AResponseInfo.ContentText := 'Hello world !'
//Sinon erreur
else AResponseInfo.ContentText := 'Unknown command !';
end; |
Partager