简体   繁体   中英

How to communicate between Flask and Javascript?

everyone. So i've been going through this documentation: http://flask.pocoo.org/docs/patterns/jquery/ . I have a Flask site up and running, but, when i try to set up what they have, i just get the error: GET http://127.0.0.1:5000/add_numbers?a=5&b=5 404 (NOT FOUND)

It just appears that the client can't even establish the connection with the server.

The only thing that differs on my side is how i assign values to the vars a and b. I've also made sure to include the script defining what $SCRIPT_ROOT is. Not sure what to do here. I also have jQuery properly loaded.

I was thinking about trying ajax, but i couldnt find a good example of ajax with python. (i'm pretty new to back end development)

$.getJSON($SCRIPT_ROOT + '/add_numbers', {
            a: 5,
            b: 5
        }, function(data) {                                 
            alert(data.result);
        });

basically, the whole point of what im trying to do is to eventually get data from a database into my javascript. I've managed to get data from my database into my HTML, but not into my JS yet. Thats what im trying.

$.getJSON is AJAX, it's just wrapped up nice and cleanly. Anyway, if you're using the server-side code from the article, then your JavaScript code has the wrong URL: you should be using $SCRIPT_ROOT + '/_add_numbers' with an _ before add_numbers .

Anyway, if you've got all the data in the HTML already, why not have the JavaScript extract the data from the HTML?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM