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
1 Answer
Reset to default 6Looks 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
版权声明:本文标题:javascript - rails xhr.send( ( options.hasContent && options.data ) || null ); error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741423667a2377950.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论