我的应用程序中有一个表单,我需要通过Ajax (JQuery)提交,但是它需要从我的JavaScript远程提交(即我不能使用:remote => true)。
我可以在我的JavaScript中找到我的表单,没有问题:
my_form = $("#the_id_of_the_form");然后创建Ajax请求:
$.post('hardcoded path?', function(data) {
// how would I serialize my parameters?
});非常感谢。
发布于 2012-08-16 07:49:56
来自jquery
示例:使用ajax请求发送表单数据
$.post("test.php", $("#testform").serialize());在你的情况下可能是这样的。
$('#your_form').submit(function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: "JSON"
});
});发布于 2012-08-16 09:01:15
不要硬编码url,而是使用存储在表单中的url。
您可以这样做:
$("#the_id_of_the_form").submit(function() {
$.post({
url: $(this).attr('action'),
data: $(this).serialize()
});
});希望这能有所帮助。
发布于 2012-08-16 07:46:50
使用jQuery(<FORM_ID>).ajaxSubmit
您的表单应该如下所示
<%= form_for @user,:url=>{:controller=>:logins, :action => 'sign_up'}, :html=>{ :onSubmit => "return checkSignupValidation()",:id=>"signup_form"} do |f|%>
<% end %>还有你的js
function checkSignupValidation()
{
jQuery('#signup_form').ajaxSubmit({
url:"/logins/sign_up",
iframe: true,
dataType: 'script',
success: function(data){
//console.log(data)
}
});
}https://stackoverflow.com/questions/11982575
复制相似问题