admin管理员组

文章数量:1289414

I trying to make like button for each recipe

but if i click like button browser said

xhr.send( ( options.hasContent && options.data ) || null );

return this errors

I don't know why this error occurred

this is my code in rails

application.js

function like(id){
    $.ajax({
        url:"/like/" + id,
        type:"POST",
        dataType:"json",
        success: function(data){
            if(data.result){
                $("#countlike").html("likes " + data.count);
                // $("#count").removeAttr('onclick');
                // $("#count").attr('disabled', 'disabled');
            }
        }
    });
}

route.rb

post '/like/:id'=>'recipes#like' 

views/recipes/show.html.erb

<p>
  <a id = "countlike" onclick="like(<%[email protected]%>)">Like it</a>
</p>

recipes_controller.rb

  def like
      likes = Recipe.find(params[:id]).likes           
      result = likes.create 
      render json:{result: result, count: likes.count}
  end

It was work correctly in other project same code

I trying to make like button for each recipe

but if i click like button browser said

xhr.send( ( options.hasContent && options.data ) || null );

return this errors

I don't know why this error occurred

this is my code in rails

application.js

function like(id){
    $.ajax({
        url:"/like/" + id,
        type:"POST",
        dataType:"json",
        success: function(data){
            if(data.result){
                $("#countlike").html("likes " + data.count);
                // $("#count").removeAttr('onclick');
                // $("#count").attr('disabled', 'disabled');
            }
        }
    });
}

route.rb

post '/like/:id'=>'recipes#like' 

views/recipes/show.html.erb

<p>
  <a id = "countlike" onclick="like(<%[email protected]%>)">Like it</a>
</p>

recipes_controller.rb

  def like
      likes = Recipe.find(params[:id]).likes           
      result = likes.create 
      render json:{result: result, count: likes.count}
  end

It was work correctly in other project same code

Share Improve this question asked Aug 16, 2017 at 9:43 PrepareForPrepareFor 2,5988 gold badges26 silver badges40 bronze badges 5
  • Check the Rails logs, there should a full trace for the 500 error, which will tell you where's the problem – a.barbieri Commented Aug 16, 2017 at 10:11
  • @a.barbieri it said Processing by RecipesController#like as JSON Parameters: {"id"=>"1"} Can't verify CSRF token authenticity – PrepareFor Commented Aug 16, 2017 at 11:41
  • Do you have <%= csrf_meta_tags %> in your view? – a.barbieri Commented Aug 16, 2017 at 11:42
  • @a.barbieri no! can i just insert that code in my view? – PrepareFor Commented Aug 16, 2017 at 11:43
  • This might be a good starting point. It should go in the head. – a.barbieri Commented Aug 16, 2017 at 11:44
Add a ment  | 

1 Answer 1

Reset to default 6

Looks like you are missing your data attribute in you post ajax request that might be throwing the error.

function like(id){
    $.ajax({
        url:"/like/" + id,
        type:"POST",
        dataType:"json",
        data: my_data, // <--- HERE
        success: function(data){
            if(data.result){
                $("#countlike").html("likes " + data.count);
                // $("#count").removeAttr('onclick');
                // $("#count").attr('disabled', 'disabled');
            }
        }
    });
}

Hope it helps. Cheers!

本文标签: javascriptrails xhrsend( ( optionshasContent ampamp optionsdata )null ) errorStack Overflow