0

I have a simple python code to test out, test.py

import random
pressure = random.random() * 8
displacement = random.random() * 70

I want to add a simple AJAX code to my .html file to execute this test.py

$.ajax({
type: "POST",
url: "~/test.py",
data: { param: text}
}).done(function( o ) {
   // do something
});

but it doesn't work. Am I doing something wrong?

Raspberry Pi, Raspbian, Apache server

1
  • have you configured your web server to execute your *.py scripts? Commented Feb 2, 2017 at 8:13

1 Answer 1

1

You could try to use Flask to serve:

your test.py code:

import random
from flask import Flask, jsonify
from flask import make_response
app = Flask(__name__)

@app.route("/test", methods=['GET', 'POST'])
def test():
    ret = {
       'pressure' : random.random() * 8,
       'displacement' : random.random() * 70,
    }
    resp = make_response(jsonify(ret))
    resp.headers.set('Access-Control-Allow-Origin',  '*')
    return resp

if __name__ == "__main__":
    app.run()

then you run it:

$ pip install Flask
$ python test.py
* Running on http://localhost:5000/

and in the ajax:

$.ajax({
type: "POST",
url: "http://localhost:5000/test",
data: { param: text}
}).done(function( o ) {
   // do something
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.