Ruby on rails: How to send an AJAX request to rails controller from login popup along with user given credentials

Also, how to use the values entered in the popup, in the javascript function: login_request() which is being called on click, which will send the AJAX request eventually.

Note: The code snippet includes all the fields enclosed in the popup.

<input class="csrf-token" type="hidden" name="authenticity_token" value="">
<div class="form-row clearfix">
  <label class="lbl-fld">Email ID</label>
  <input type="email" name="user[email]" ng-model="loginForm.email" placeholder="[email protected]" ng-required="true"/>
  <!--<span class="valid-chk">-->
  <!--<i ng-class="{'false':'icon-close', 'true': 'icon-correct'}[form['user[email]'].$valid]"></i>-->
  <!--</span>-->
</div>
<div class="form-row clearfix">
  <label class="lbl-fld">PASSWORD</label>
  <input type="password" name="user[password]" placeholder="password" ng-required="true"/>
  <!--<span class="valid-chk">-->
  <!--<i ng-class="{'false':'icon-close', 'true': 'icon-correct'}[form['user[password]'].$valid]"></i>-->
  <!--</span>-->
</div>
<div class="btn-container clearfix">
  <div class="btn-wrap">
    <input type="submit" name="commit" value="LOGIN" ng-click="login_request()" />
  </div>
  <div class="loader-wrapper" ng-show="loading" style="bottom: 25px;left: -53px;position: absolute;width: 25px;">
    <img class="loader-img-common" src="<%= asset_path('loader.gif') %>" style="max-width: 100%;" />
  </div>
</div>

Thanks.

Answers:

Answer
 function login_request(data_to_send)
 {
   $.ajax({
   type: 'post',
   url: 'controller/action',
   data: { key: "value" key_1: "value_1" },
   success: function(data) {
   // paste your code here after success
   }
  });
}  


  #in your config/route.rb

      match 'controller/action', :to => 'controller#action' ,:via => [:get,:post] # i'm using get as well as post also.

  #hope that help you. if you face any problem please let me know

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.