Ich möchte einige JSON-Daten von jQuery an flask übergeben. Ich habe eine add-user-Methode, die einige Formularwerte übernimmt und an flask übergibt.Weitergabe von JSON-Daten von jQuery an den Kolben
function kullaniciEkle(event) // add a user
{
event.preventDefault();
var hataSayisi = 0;
$('#addUser input').each(function (index, val) {
if ($(this).val() === '') {
hataSayisi++;
}
});
console.log("Number of errors: ", hataSayisi);
if (hataSayisi === 0) {
var yeniKullanici = {
'kullanıcıAdı' : $('#kullaniciEkle fieldset input#girisIsim').val(),
'eposta' : $('#kullaniciEkle fieldset input#girisEposta').val(),
'tamİsim' : $('#kullaniciEkle fieldset input#girisTamIsim').val(),
'yaş': $('#kullaniciEkle fieldset input#girisYas').val(),
'yer' : $('#kullaniciEkle fieldset input#girisSehir').val(),
'cinsiyet' : $('#kullaniciEkle fieldset input#girisCinsiyet').val()
}
console.log(yeniKullanici);
console.log(JSON.stringify(yeniKullanici));
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(yeniKullanici),
url: '/add',
success: function(data){
console.log("Are we here?");
$('#kullaniciEkle fieldset input').val('');
},
error: function(xhr, textStatus, error){
alert(xhr.responseText);
},
dataType: "json"
});
} else {
alert("Please fill in all form fields");
}
}
Und die Add-Methode in der Flasche ist so.
@app.route('/add', methods=['POST'])
def addUser():
if request.method == "POST":
content = request.get_json(silent=True)
print (content)
return 'Hi there'
Es sieht so aus, als könnte ich die JSON-Daten aus der Flasche bekommen. Aber auf der jQuery-Seite wird ein Fehlerteil ausgeführt. Weil ich eine Warnmeldung 'Hi there' bekomme, wenn die Funktion addUser() diesen Wert zurückgibt oder wenn sie eine leere Warnmeldung zurückgibt, wenn der zurückgegebene Wert leer ist.