2013-03-14 68 views
0

我試圖檢查窗體如果新用戶的電子郵件地址已存在。jQuery驗證遠程選項燒瓶

這是我的HTML:

$("#createform").validate({ 
      errorClass: "errormessage", 
      validClass: "success", 

     rules: { 
      fname: { 
       required: true, 
       minlength: 2, 
       string: true, 
      }, 

      lname: { 
       required: true, 
       minlength: 2, 
       string: true, 
      }, 

      email: { 
       required: true, 
       email: true, 
       remote: $.getJSON($SCRIPT_ROOT + "/_check_mail") 
      }, 

      password: { 
       required: true, 
       minlength: 5, 
      }, 

      confirmpassword: { 
       equalTo: "#password" 
      } 
     } 

這是我的瓶法:

@app.route('/_check_mail') 
def check_mail(): 
    mail = request.args.get('email') 
    check = database.check_mail(mail) 
    return check 

其中database.check_mail(郵件)檢查電子郵件是已經在數據庫中並返回true或False(使用JSON)。

當我第一次加載頁面和客戶端嘗試存取權限燒瓶URL(/ _check_mail)工作原理:

  • 請求URL:http://0.0.0.0:5000/_check_mail
  • 請求方法:GET狀態
  • 代碼:200 OK

但是,當我輸入一個電子郵件,我發送該請求到得到了404個respons服務器:

  • 請求URL:http://0.0.0.0:5000/[object%20Object]?email=arnoutaertgeerts%40gmail.com
  • 請求方法:GET
  • 狀態代碼:404 NOT FOUND

所以我認爲這是一起發送的電子郵件有問題?有任何想法嗎?

回答

0

問題解決了!我用我的要求:

email: { 
    required: true, 
    email: true, 
    remote: function(request, response) { 
     $.getJSON($SCRIPT_ROOT + "/_check_mail", { 
     email: $('#email').val() 
     }, response); 
} 

現在一切工作,除了顯示錯誤信息。當電子郵件已存在時,我會返回帶有錯誤消息的字符串(如文檔中所述),但消息不會顯示。但是,當我看到這些敏感的反應時,我清楚地接受了它!

+0

答案只應包含解決方案,後續問題應修改爲您的OP。你指的是什麼文件? [我只在這裏看到兩個例子,而且沒有一個](http://docs.jquery.com/Plugins/Validation/Methods/remote#examples)與你的方法非常相似。 – Sparky 2013-03-14 21:55:17

+0

這就是我提到的文檔,我試圖用php這樣做,但它沒有奏效。並且使用遠程規則,您應該在您的respons中返回錯誤文本:) – arnoutaertgeerts 2013-03-15 17:08:53

+0

爲什麼不在'remote'規則中使用標準的通用錯誤消息?它是否來自您的遠程腳本? – Sparky 2013-03-15 21:03:16