繁体   English   中英

Ajax响应与Rails响应不成功

[英]ajax response not success with rails respond to

我有以下javascript代码

$(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")
        }
      });
  });
});

和以下控制器代码

  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

每当我从客户端发出此呼叫时,服务器都会收到错误响应,但我不知道为什么? 看来我已经完全按照应该做的工作了。 请帮助返回成功的答复,以便我可以用评论更新dom。

谢谢。

首先,尝试在哈希中使用“ url”:

$.ajax({
  url: url,
  method: 'post',
  data: body,
  dataType: 'json',
  success: function(response) {
    alert("got success")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM