简体   繁体   中英

How can i send slider value from jQuery or javascript to python Flask?

I have a round slider with the slider value on client side or web page. when the user changes the slider position, the client has to send the value to the server. Here i use python flask as a server side. So the value has to be sent from j Query or java script to flask. I tried with the following code. But when i use Ajax, the web page becomes blank. It doesn't show the slider. If i remove the Ajax way of sending, the slider appears but value is not sent to server.

CLIENT SIDE:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery roundSlider - JS Bin</title>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/jquery.roundslider/1.0/roundslider.min.css">
<script src="//cdn.jsdelivr.net/jquery.roundslider/1.0/roundslider.min.js"></script>
</head>   
<body>
<div id="slider"></div>
<div id="slide"></div>
<script>
$(document.ready(function(){
var value;
$("#slider").roundSlider({
    sliderType: "min-range",
    change: function(){
    var obj1 =  $("#slider").data("roundSlider");
    value = obj1.getValue();
$("#slide").html(value);
});
$.ajax({
    type: 'POST',
    url: "{{url_for('test')}}",
    contentType: 'application/json;charset=UTF-8',
    data: {'data':value}
});
});

</script>
</body>

</html>

SERVER SIDE:

    def flask():
            connection()
            app = Flask(__name__, template_folder='Templates')
            @app.route('/test/', methods=['GET', 'POST'])
            def test():
                    if request.method == "POST":
                            value=request.json['data']
                            print(value)
                    return render_template('roundslider1.html')

            if __name__ == "__main__":
                    app.run(host='192.168.42.1',port=2030, debug=True)

After reading some more documentations, i used the below code to send the variable from j Query to flask server.

CLIENT SIDE:

var value;
$("#slider").roundSlider({
  sliderType: "min-range",
  change: function(){
  var obj1 =  $("#slider").data("roundSlider");
  value = obj1.getValue();
  $.getJSON('/valueofslider', {
    a:value
  });

SERVER SIDE:

@app.route('/valueofslider')
def slide():
    a = request.args.get('a')
    print (a)

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