I have the following javascript code
$(document).ready(function() {
$('#comment_form').on('click', '.button', function() {
url = document.URL
body = $(this).find('#comment_body').text()
$.ajax(url, {
method: 'post',
data: body,
dataType: 'json',
success: function(response) {
alert("got success")
},
error: function(response) {
debugger;
alert("got an error")
}
});
});
});
and the following controller code
def create
@comment = commentable.comments.new(comment_params)
@comment.user = current_user
respond_to do |format|
if @comment.save
format.json { render json: @comment }
else
format.json { render json: @comment.errors }
end
end
end
Whenever I make this call from my client I get a error response from my server and I do not know why? It looks like I have followed exactly what is supposed to be done. Please help to return a successful response so i can update the dom with the comment.
Thank you.
To start off, try with 'url' inside the hash:
$.ajax({
url: url,
method: 'post',
data: body,
dataType: 'json',
success: function(response) {
alert("got success")
}
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.