Accessing Flask server from my web page

后端 未结 1 438
予麋鹿
予麋鹿 2021-01-27 19:35

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

相关标签:
1条回答
  • 2021-01-27 20:11

    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.

    0 讨论(0)
提交回复
热议问题