简体   繁体   English

使用AJAX和Rails提交表单

[英]Submitting form with AJAX and Rails

Right now I'm struggling a little bit with getting my form to submit with AJAX. 现在,我在使表格向AJAX提交方面有些挣扎。 Eventually I will be changing some DIVs on success, but right now I would just like to get it working. 最终,我将根据成功更改一些DIV,但现在我只是想让它工作。 My code for the form looks like this: 我的表单代码如下所示:

<div class="modal signUpContent fade" id="ModalLogin" tabindex="-1" role="dialog" >
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button>
        <h3 class="modal-title-site text-center" > Login  to All For Funds </h3>
      </div>
      <div class="modal-body">
        <div id="login_form">
        <form accept-charset="UTF-8"  class="new_user" id="loginForm" name="loginForm" action="">
          <div style="display:none">
          <input name="utf8" type="hidden" value="✓">
          <input name="authenticity_token" type="hidden" value="iapD/wScrrV7ga6YEXGxlI1OZ2sVgfEUsA5RzibKpFw=">
        </div>
        <div class="form-group reg-email">

          <div>
            <input autofocus="autofocus" id="user[email]" name="user[email]" type="email" value="" class="form-control input"  size="20" placeholder="Enter Email">
          </div>
        </div>
        <div class="form-group reg-password">
          <div >
            <input autocomplete="off" id="user[password]" name="user[password]" type="password" class="form-control input"  size="20" placeholder="Password">
          </div>
        </div>
        <div class="form-group">
          <div >
            <input name="user[remember_me]" type="hidden" value="0">
            <div class="checkbox login-remember">
              <label>
                <input id="user[remember_me]" name="user[remember_me]" checked="checked" type="checkbox">
                Remember Me </label>
            </div>
          </div>
        </div>
        <div >
          <div >
            <input name="login-button" id="login-button" class="btn btn-block btn-lg btn-primary" value="LOGIN" type="submit" onS>
          </div>
        </div>
      </form>
    </div>
        <!--userForm--> 

      </div>
      <div class="modal-footer">
        <p class="text-center"> Not here before? <a data-toggle="modal"  data-dismiss="modal" href="product-details.html#ModalSignup"> Sign Up. </a> <br>
          <a href="forgot-password.html" > Lost your password? </a> </p>
      </div>
    </div>
    <!-- /.modal-content --> 

  </div>
  <!-- /.modal-dialog --> 

</div>
<!-- /.Modal Login --> 

And my JS looks like this: 我的JS看起来像这样:

$(document).ready(function(){

$("#loginForm").submit(function(e) {

$.ajax({
       type: "POST",
       url : "/users/sign_in",
       data: $("#loginForm").serialize(), // serializes the form's elements.
       success: function(data)
       {
           alert('Posting!');
            $('.navigation').load('/application_controller/login');
            e.preventDefault();
            return;
       }
     });

});

});

Any suggestions would be great 任何建议都很好

Even though you've left action blank and ommitted method on your form element, it'll still default to the current URI and GET , respectively, meaning you aren't stopping the form from doing it's default behavior. 即使您在form元素上保留了空白action和省略的method ,它仍将分别默认为当前URI和GET ,这意味着您不会阻止该表单执行其默认行为。

Your AJAX success handler will only be called after the AJAX request is successful (returns from server with 2xx HTTP code). 仅在AJAX请求成功后(从具有2xx HTTP代码的服务器返回),才会调用AJAX success处理程序。 Because your form is submitting as normal, the AJAX request probably never completes to get to where you're trying to prevent the default action. 由于您的表单是正常提交​​的,因此AJAX请求可能永远无法完成以到达您尝试阻止默认操作的位置。

When you handle the submit event, you'll want to prevent the default action immediately before starting your AJAX request. 当您处理submit事件时,您将希望在开始AJAX请求之前立即采取默认操作。

$('#loginForm').submit(function(e) {
  e.preventDefault();
  $.ajax(...)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM