2012-08-12 93 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