Flask - AJAX fetches NONE value request.args.get



我的python代码返回错误:

TypeError: JSON对象必须是str,而不是'NoneType'

Ajax能够从我的html文件中加载数据并发送请求。我的python代码没有接收到这个值。

AJAX代码

$(function() {
$("#forgot").submit(function(event) {
     var em = document.getElementById('email').value;
     var ph = document.getElementById('phone_number').value;
     $.ajax({
        type: "POST",
        url: $SCRIPT_ROOT + "/forgot_password",
        data: JSON.stringify({
            'email' : em,
            'phone' : ph
        }),
        dataType: 'json',
        success: function(data) {
            console.log("hi");
            if(data.status == "success")
            { alert("Your password has been sent to your registered email"); }
            else{
                 alert("Invalid Credentials");
            }
        },
         error:function(exception){
            alert(exception);
            console.log('Exeption:'+exception);
            }
    });
});
});

我的服务器代码:

@app.route('/forgot_password', methods=["GET","POST"])
def forgot_password():
form = LoginForm()
if request.method == "POST":
    _email = json.loads(request.form.get('email'))
    _phone = json.loads(request.form.get('phone'))
    print(_email['value'])
    print(_phone)
    check_email = User.query.filter_by(email=_email,phone=_phone).first()
    if (check_email != None):
        return(jsonify({"status":"success"}))
    else :
        return(jsonify({"status":"fail"}))
return(jsonify({"status":"fail"}))

问题是您试图获得GET值而不是获得POST值。
您的代码应该看起来像request.form['email']而不是request.form.get('email')
,因为您的XMLHttpRequest是POST 而不是 GET