簡體   English   中英

AJAX發布方法中的400錯誤響應錯誤

[英]400 Bad Response Error in AJAX Post Method

我正在嘗試通過AJAX請求向鏈接(這是使用Rails和JavaScript / JQuery構建的模擬Reddit應用程序)中添加注釋,以避免整個頁面加載(我不能在此應用程序中使用remote:true)。

我可以添加注釋,並通過Rails將注釋列表附加到它們的后面,但是當我嘗試使用AJAX方法時,出現400錯誤的請求錯誤。

這是我的腳本:

`

function submitViaAjax() {
    $("#new_comment_button").on("click", function (e) {
        url = this.action
        //var commentText = document.getElementById("comment_body").innerHTML
        //var myJSON = JSON.stringify(commentText);

        data = {
            'authenticity_token': $("input[name='authenticity_token']").val(),
            'comment': {
                'content': $("#comment_body").val()
            }
        };

        console.log(data);

        $.ajax({
            type: "POST",
            url: url,
            data: data,
            headers: { 'Content-Type': 'application/json' },
            success: function (response) {
                var $ul = $("div.comments_section ul");
                $ul.append(response)

            }
        })
        e.preventDefault();
    })
};

這是具有以下形式的“鏈接”顯示頁面:

<div class="comments_section">
   <%= render 'comments/comments' %>
</div>

<!--<div id="comments">
</div> -->

<%= simple_form_for [@link, Comment.new]  do |f| %>
  <div class="field">
    <%= f.text_area :body, class: "form-control" %>
  </div>
  <br>
  <%= f.submit "Add Comment", class: "btn btn-primary", id: "new_comment_button", data: { disable_with: false } %>
<% end %>

任何見識將不勝感激。

更新:我的控制器代碼,每個請求:

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!

  def index
    if params[:link_id]
      @link = Link.find(params[:link_id])
      @comments = @link.comments
    else
      @comments = Comment.all
    end

    respond_to do |format|
      format.html { render :index }
      format.json { render json: @comments }
    end
  end

  def create
    @link = Link.find(params[:link_id])
    @comment = @link.comments.new(comment_params)
    @comment.user = current_user

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @link, notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
        render 'comments/show', :layout => false
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment.destroy
    respond_to do |format|
      format.html { redirect_back fallback_location: root_path, notice: 'Comment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:link_id, :body, :user_id)
    end
end

與@Taplar所說的相呼應,# #new_comment_button元素似乎沒有action屬性。 這實際上是在某處編碼的嗎? 同樣,該action屬性必須是您的AJAX“服務”的確切值-是否正確設置?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM