Rails jquery ajax call not firing

244 views Asked by At

I need to get a braintree token using ajax but its not firing. Normally I would generate the token in the new action but my orders form is a partial in a listing show page and thus I only have a create action. The token is needed for the braintree dropin to work. so I figured my only other option is to attach an ajax call to the checkout button but it wont fire. Would appreciate help on why the ajax is not working or even a better method of getting the token

routes.rb

 get '/token' => 'orders#token' 

Then in my controller

class OrdersController < ApplicationController      

def token
    @token = Braintree::ClientToken::generate() 
    render json: @token
end 

in my html

<a class="btn btn-large waves-effect cyan waves-light modal-trigger" style="width: 300px;  margin-left: -65px;" id="btn-book" href="#modal1">Proceed to Checkout</a>


<script>
      $('#btn_book').on('click', function(e){
        e.preventDefault();

        $.ajax({
            type: 'GET',
            url: '/token',
            dataType: 'JSON',
            success: function(data){
                 console.log(data);
            },
            error: function(data){
                console.log('there was an error');
            }
        });

      });                        

</script>    
1

There are 1 answers

1
itdoesntwork On

The id of your button is btn-book so you should be using $("#btn-book"), not $("#btn_book").