souravlahoti
souravlahoti

Reputation: 736

Flask - AJAX fetches NONE value request.args.get

My python code returns error :

TypeError: the JSON object must be str, not 'NoneType'

Ajax is able to load the data from my html file and sends the request. My python code doesn't receives the value.

My AJAX code

$(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);
            }
    });
});
});

My server code :

@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"}))

Upvotes: 0

Views: 1424

Answers (1)

magic-sudo
magic-sudo

Reputation: 1246

Problem is you are trying to get GET values instea of getting POST value.
Your code should look like request.form['email'] not request.form.get('email')
because your XMLHttpRequest is POSTnot GET.

Upvotes: 1

Related Questions