This is a question continue from this question here.
I am trying to control a servo motor using the image buttons on my web page. My servo controller is in the form of p
You'll run into the same-origin policy restrictions unless you serve the index.html
file from the same host and port number. It's easiest to just add the index.html
page to your Flask server too.
Add a /
route that serves the page that will do the AJAX post. You could use a template to fill in the route here for $.post()
to. If using Jinja2 for the template, that would be:
@app.route('/')
def homepage():
return render_template('index.html')
and the file index.html
in the templates
subdirectory of your Flask project with:
$('#left_button').click(function(){
$.post("{{ url_for('turn_servo_ajax') }}", {direction:"left"}).done(function (reply) {
$('#camerapos').empty().append(reply);
alert("left button clicked");});
});
where the {{ }}
part is Jinja2 template syntax, and url_for() returns a fully-formed URL for the turn_servo_ajax
view function.