Forum > Networking and Web Programming
Http Server
PierceNg:
--- Quote from: michoux on December 05, 2022, 04:33:44 pm ---Here is simple code.
I also have a form with lazreport on it this is all.
I just want to open report with
form1.frReport1.LoadFromFile('test.lrf');
form1.frReport1.ShowReport;
--- 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";}};} --- program Project1; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes,SysUtils, fphttpapp, httpdefs, httproute, fpjson, jsonparser, interfaces,forms,Unit1; type TWebServerThread = class(TThread) protected procedure Execute; override; public constructor Create(CreateSuspended: boolean); end; constructor TWebServerThread.Create(CreateSuspended: boolean); begin inherited Create(CreateSuspended); FreeOnTerminate := true; end; procedure TWebServerThread.Execute; begin fphttpapp.Application.Run; end; procedure test(req: TRequest; res: TResponse);var s:string; rawJson: AnsiString; people: TJSONArray; person: TJSONObject; personEnum: TJSONEnum;begin // Get the JSON data rawJson:=req.Content; writeln(rawjson); form1.frReport1.LoadFromFile('test.lrf'); form1.frReport1.ShowReport;end; begin fphttpapp.Application.Port := 8000; HTTPRouter.RegisterRoute('/test', @test, true); fphttpapp.Application.Threaded := true; fphttpapp.Application.Initialize; TWebServerThread.Create(false); RequireDerivedFormResource:=True; Forms.Application.Scaled:=True; Forms.Application.Initialize; Forms.Application.CreateForm(TForm1, Form1); Forms.Application.Run; end.
--- End quote ---
According to the wiki page on multi-threaded programming I linked to earlier, section 'Lazarus Widgetset Interfaces':
--- Quote ---Only one thread in an application should call LCL APIs, usually the main thread. Other threads can make use of the LCL through a number of indirect methods, ...
--- End quote ---
So your '/test' web handler shouldn't do LCL GUI things directly. Instead, it should save the data received, and somehow inform the main thread to process the saved data. See the wiki page for ideas.
egsuh:
Hi michoux,
I think here's an example you are looking for.
https://www.freepascal.org/~michael/articles/webserver1/webserver1.pdf
You can also find the sources in the following list. Scroll down the list until you find webserver.
https://www.freepascal.org/~michael/articles/
michoux:
Hi
Thank you for your help. I managed to get things going.
Now next step is to read some data and parse it.
I got following script which sends some data to serve.
--- 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";}};} ---<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script></head> <body> <h1>Localhost service call test</h1> <label for="url-call">URL to call:</label> <input style="width: 400px;" type="text" id="url-call" name="url-call" value="https://127.0.0.1:8000/sandbox"><br><br> <a id="test-call" href="#">Call</a> <p id="odgovor"></p></body> <footer> <script type="text/javascript"> $("#test-call").click(function(){ $.ajax({ type: 'POST', url: $("#url-call").val(), dataType: 'json', data: { "id": 10, "title": "HP Pavilion 15-DK1056WM", "description": "HP Pavilion 15-DK1056WM Gaming...", "price": 1099, "discountPercentage": 6.18, "rating": 4.43, "stock": 89, "brand": "HP Pavilion", "category": "laptops", "thumbnail": "https://i.dummyjson.com/data/products/10/thumbnail.jpeg", "images": [ "https://i.dummyjson.com/data/products/10/1.jpg", "https://i.dummyjson.com/data/products/10/2.jpg", "https://i.dummyjson.com/data/products/10/3.jpg", "https://i.dummyjson.com/data/products/10/thumbnail.jpeg" ] }, success: function (r) { $("#odgovor").html("Odgovor: <br> Success: <br> " + JSON.stringify(r, null, 2)); }, error: function (r) { $("#odgovor").html("Odgovor: <br> Error: <br> " + JSON.stringify(r, null, 2)); } }, $(this)); }); </script></footer> </html>
I get the data but it is is not in real json format it is some strange outpout.
Here is the received data.
"id=10&title=HP+Pavilion+15-DK1056WM&description=HP+Pavilion+15-DK1056WM+Gaming...&price=1099&discountPercentage=6.18&rating=4.43&stock=89&brand=HP+Pavilion&category=laptops&thumbnail=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2Fthumbnail.jpeg&images%5B%5D=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F1.jpg&images%5B%5D=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F2.jpg&images%5B%5D=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F3.jpg&images%5B%5D=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2Fthumbnail.jpeg"
Regards Matija
michoux:
I think above is problem with ajax code posting Json, I will try to fix this
Navigation
[0] Message Index
[*] Previous page