I would like to get the position data from the js joystick o use it at the flask server. I tried using ajax:
python:
@app.route(‘/test’, methods=[‘GET’, ‘POST’])
def test_ajax():
if request.method == "POST":
data = request.json
print('data:', data)
result = {'url': url_for('index')}
print('result:', result)
return jsonify(result)
else:
return render_template('index.html')
<div class="container fluid">
<h1>Joystick</h1>
<div id="joy1Div" style="width:200px;height:200px;margin-bottom:20px;"></div>
</div>
<script type="text/javascript">
var Joy1 = new JoyStick('joy1Div', {}, function(stickData) {
joy1IinputPosX.value = stickData.xPosition;
joy1InputPosY.value = stickData.yPosition;
joy1Direzione.value = stickData.cardinalDirection;
joy1X.value = stickData.x;
joy1Y.value = stickData.y;
});
</script>
<script>
data = {'ID': 'foobar'};
var joystick = document.getElementById('joy1Div');
joy1X.onchange = function() {
$.ajax({
type: "POST",
url: "/test",
contentType: 'application/json;charset=UTF-8',
data: JSON.stringify(data, null, 't'),
dataType: 'json',
success: function(data) {
window.location.href = data['url']
},
error: function(response) {
alert(response)
},
});
};
</script>
Every time the position of the joystick changed I would like to send the position data to flask