簡體   English   中英

Ajax Bad Request 400 Rails

[英]Ajax Bad Request 400 Rails

我在Rails上使用Ajax收到錯誤的請求錯誤400。 當我提交我的表單時,我有一個字符串作為參數從Jquery發送,我想從params [:assignee]檢索它,所以我可以提取字符串並通過我的控制器保存。 我的控制器:

    def create
    @task = Task.new(task_params)
    @task.user = current_user
    username = params.permit[:assignee]
    @task.assignee = username

    #set_category
    respond_to do |format|
      if @task.save
        format.html { redirect_to tasks_url, notice: 'Task was successfully created. '+task_params.inspect}

        #format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render :show, status: :created, location: @task }
      else
        format.html { render :new }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

def task_params
  params.require(:task).permit(:owner, :value, :completed, :category, :date, :assignee)

end

這是我的JS:

 $( "#new_task" ).submit(function() {
    alert("form: "+assignee);
  //event.preventDefault();
 $.ajax({
   url: "/tasks",
   type: "POST",
   data: {assignee},
   dataType: "json",
   success: function(data) {
       alert('successfully');
     },
    error: function(xhr, textStatus, error) {
     alert(xhr.statusText+""+textStatus+""+error);
  }
   });
});

assignee是在jquery自動完成表單中選擇的用戶名:

select: function(event, ui) {
    var terms = split(this.value);
    // remove the current input
    terms.pop();
    // add the selected item
    terms.push(ui.item.value);
    // add placeholder to get the comma-and-space at the end
    terms.push("");
    this.value = terms.join("");
    assignee=this.value;
    $('input[name=commit]').prop("disabled",false);

    return false;
}

我的根是“任務/”,您可以在其中查看已保存的任務和創建新任務的表單。 我在網上搜索了很多,我試了一下。 我能怎么做? 非常感謝

400錯誤請求 - 由於明顯的客戶端錯誤(例如, 格式錯誤的請求語法 ,太大的大小,無效的請求消息框架或欺騙性請求路由),服務器無法或不會處理請求。

維基

ajax代碼更改為:

$.ajax({
   url: "/tasks",
   type: "POST",
   dataType: "json",
   headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'), // Optional
      'Content-Type': 'application/json'
   },
   data: JSON.stringify({ assignee: assignee }),
   success: function(data) {
       alert('successfully');
   },
   error: function(xhr, textStatus, error) {
     alert(xhr.statusText+""+textStatus+""+error);
   }
});

{assignee}這是一個無效的JSON對象應該是{assignee: assignee}你還應該添加一個有效的標題, 'Content-Type'和( X-CSRF-TOKEN可選)

解決了!

    $( "#new_task" ).submit(function(event) {
    alert("form: "+assignee);
    var value = $('#new_task').find('input[name="task[value]"]').val();
event.preventDefault();
 $.ajax({
   url: "/tasks",
   type: "post",
   contentType: "application/json",

   data: JSON.stringify({ assignee: assignee, value: value }),
   success: function(data) {
       alert('successfully');
   },
   error: function(xhr, textStatus, error) {
     alert(xhr.statusText+" "+textStatus+" "+error);
   }
});

});

event.preventDefault(); - >沒有這個,表格提交兩次。

var value = $('#new_task').find('input[name="task[value]"]').val(); - >沒有這個,我可能會因為提醒任務#create的“post tasks”而失去我的表單值

暫無
暫無

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

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