Triggering Python Script Using Ajax Javascript Request On Local Server Using Vanilla Js
Solution 1:
SimpleHTTPServer only serves file content. If you want to execute the script and return the output, you need CGIHTTPServer.
Note that both modules have been merged into http.server in python 3.
Solution 2:
You need to change the line:
xhttp.open("GET", "./request.py", true);
to xhttp.open("GET", "http://localhost:3333/<your-route>", true);
. <your-route>
is defined in your python file and I assume the python web server is running on port 3333.
If you are using Flask and define a route using for example: @app.route("/endpoint")
,
the request should look like:xhttp.open("GET", "http://localhost:3333/endpoint", true);
Solution 3:
You can use SocketServer instead of the default server. Add a handler to the SocketServer. Instead of a GET, do a POST with the actual command to run. The handler can check the post request and run the required script.
Post a Comment for "Triggering Python Script Using Ajax Javascript Request On Local Server Using Vanilla Js"