简体   繁体   中英

Getting a Syntax Error While doing a ajax post form

I have two div containers. One div has a form and the other has a lot of sensitive content that I would like to hide. They are both built with bootstrap.

The idea is that the first form requires a person to enter a password and then this should trigger a ajax post call to the backend to check if the password is correct and if it is, the second container will be revealed.

in my routes.rb

post 'users/trial_signup_validation', to: 'users#trial_signup_validation' 

in my users_controller.rb

  def trial_signup_validation
    @password = params['password']
    if @password == 'sales'
      render :json => { "success" : "true" }
    else 
      render :json => { "success" : "false" }
  end 

The method takes the object from the ajax post and check it, then return a json true or false back to the view.

Here is the view

<script type="text/javascript">

$("#protectivepanel").submit(function(event) {

  $.ajax({
        type: "POST", 
        url: "/users/trial_signup_validation", 
        data: $("#protectivepanel").serialize(),
        success: function(data){
          return data.success 
        } 
    }); 
  event.preventDefault()
});


$(document).ready(function(){
    $("#the_submit_button").click(function(e){ 
      e.preventDefault();
        if (data.success){
          $("#protectivepanel").hide() 
          $("#salespanel > div").removeClass("hide")
        }
        else {
          $("#protectivepanel").show()
        }   
    });
  });
</script>

my first form looks like this

<div class="container" id="protectivepanel">
  <form role="form">
    <div class="form-group">
      <label for="representativepassword">Representative's Password</label>
      <input type="text" class="form-control" id="representativepassword" placeholder=" Enter Password">
      <button type="submit" class="btn btn-default" id="the_submit_button">Submit</button>
    </div>
  </form>
</div>

also when I run rails s , I am getting a syntax error pointing to another method in the same controller which makes no sense to me.

SyntaxError at /users/trial_signup
syntax error, unexpected ':', expecting => 

this method works perfectly before I started working on the jquery form posting.

Update trial_signup_validation as below:

 def trial_signup_validation
    @password = params['password']
    if @password == 'sales'
      render :json => { "success" => "true" }  ## Use hash rocket instead of :
    else 
      render :json => { "success" => "false" }  ## Use hash rocket instead of :
  end 

NOTE:

"success" and "true" are both Strings so you will have to use hash rocket(=>) to denote hash syntax, you cannot use : because there is no symbol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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