1
我有一個圓滑塊與客戶端或網頁上的滑塊值。當用戶更改滑塊位置時,客戶端必須將該值發送到服務器。在這裏我使用python燒瓶作爲服務器端。所以值必須從j Query或java腳本發送到瓶子。我試着用下面的代碼。但是當我使用Ajax時,網頁變成空白。它不顯示滑塊。如果我刪除發送的Ajax方式,則會出現滑塊,但值不會發送到服務器。如何將jQuery或JavaScript的滑塊值發送到python Flask?
客戶端:
<!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>
服務器端:
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)