2012-08-12 91 views
0

我想提交一個表單使用jjRoutes在Ajax後首先使用jQuery表單驗證插件進行驗證輸入。問題是,當我將「data:form」添加到ajax調用的參數時,響應處理程序從不會被調用。一些代碼進一步解釋問題:ajax提交表單在播放框架

jsRoutes.controllers.Application.authenticate().ajax({ 
    data : { 
     form : this //this messes things up! 
    }, 
    success : function(data) { 
     window.location = "/"; //never called with the above data stuff 
    }, 
    error : function(err) { 
     alert(err); //never called with the above data stuff 
    } 
}); 

我能夠讀取控制器中的表單域。當然,一種解決方案是將表單中的每個字段手動提取到數據部分(如下所示),但這不是期望的。還有其他解決方案嗎?

data : { 
    username : <username from form> 
    password : <password from form> 
}, 

回答

3

設置一個唯一的ID屬性爲您<form>標籤即:<form id="my_form" ...>後來使用ID作爲一個jQuery選擇它序列:

jsRoutes.controllers.Application.authenticate().ajax({ 
    data : $("#my_form").serialize(), 
    success : function(data) { 
     // ... 
    }, 
    error : function(err) { 
     // ... 
    } 
}); 

使用一些瀏覽器檢測工具(即FireBug)至檢查它是否發送你想要的,如果響應是formatted根據需要

+0

謝謝!那正是我想念的東西!); – 2012-08-12 12:11:29