javascript - Using info from GET request in jQuery to display transformed result through Flask -
i'm trying send client info based on selection make. i'd use request since i'm not changing info on server. however, don't know how access info request; i've been researching using querystrings flask haven't had luck. need manipulation result after comes javascript, i'd keep response in success function have below rather use sort of templating.
the things afford change how data gets sent (if needs json or besides string) , how gets accessed in flask. possible , how it?
app.py
from flask import flask, render_template, request, jsonify app = flask(__name__) def myfunc(s): return str(s) + "!" @app.route("/") def index(): # resp = myfunc(selectedoption) # return resp if __name__ == '__main__': app.run(debug=true)
index.html
<!doctype html> <html> <head> <title>gettest</title> <!-- latest compiled , minified css --> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <!-- jquery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <!-- latest compiled javascript --> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <script src="{{ url_for('static', filename='script.js')}}"></script> </head> <body> <select id="options"> <option value="option1">option 1</option> <option value="option2">option 2</option> <option value="option3">option 3</option> <option value="option4">option 4</option> </select> <button class="btn btn-default" id="submit">submit</button> <p id="demo"></p> </body> </html>
script.js
$(document).ready(function() { $("#submit").click(function() { var selectedoption = $("#options").val(); $.ajax({ url: "/", method: "get", data: selectedoption, //open changing success: function(result) { $("#demo").html(result); } }); }); });
you should alter ajax call uses named parameter rather setting data equal options
$.ajax({ url: "/", method: "get", data: {'selectedoption': selectedoption}, //open changing success: function(result) { $("#demo").html(result); } });
access querystring request.args.get('selectedoption')
like
@app.route("/") def index(): resp = myfunc(request.args.get('selectedoption')) return resp
Comments
Post a Comment