Dongkun Lee Dongkun Lee - 2 months ago 20
Ajax Question

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

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(<%=@recipe.id%>)">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

Answer Source

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!