Python:
Код: Выделить всё
@app.route('/receive-data', methods=['POST'])
def receive_data():
data = request.get_json() # Use request.get_json() to explicitly parse JSON
if data is None:
return jsonify({"error": "Invalid content type or empty payload"}), 400
variable = data.get('variable')
print(f"Received variable from JavaScript: {variable}")
return jsonify({"status": "success", "received_variable": variable})
if __name__ == '__main__':
app.run(debug=True)
Код: Выделить всё
var audioControls = document.getElementById("audioControls");
let currentSong = null
let audioElement = document.getElementById("audio");
audioElement.src = currentSong
let beatcheckbox = false
let clonecheckbox = false
document.getElementById("beatcb").addEventListener("change", function() {
var bcheckbox = document.getElementById("beatcb");
// Check if the checkbox is checked
if (bcheckbox.checked) {
console.log("beat checkbox is checked");
beatcheckbox = true
// The variable you want to send to Python
const myVariable = beatcheckbox;
// Send the variable using fetch
fetch('http://127.0.0.1:5000/receive-data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Charset': 'UTF-8',
'Accept': 'application/json'
},
body: JSON.stringify({ variable: myVariable })
})
.then(response => {
if (!response.ok) {
return response.text().then(text => { throw new Error(text) });
}
return response.json();
})
.then(data => {
console.log('Response from server:', data);
})
.catch((error) => {
console.error('Error:', error);
});
} else {
console.log("beat checkbox is not checked");
beatcheckbox = false
}
});
document.getElementById("clonecb").addEventListener("change", function() {
var clonecbox = document.getElementById("clonecb");
if (clonecbox.checked) {
console.log("clone checkbox is checked");
clonecheckbox = true
} else {
console.log("clone checkbox is not checked");
clonecheckbox = false
}
});
Подробнее здесь: https://stackoverflow.com/questions/790 ... ent-type-w