2015-07-12 33 views
0

我的Python代碼返回錯誤:瓶 - AJAX獲取NONE值request.args.get

TypeError: the JSON object must be str, not '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"})) 

回答

1

問題是你試圖讓越來越POST價值GET值instea。
您的代碼看起來應該像request.form['email']request.form.get('email')
因爲你的XMLHttpRequest是POSTGET