javascript - Ajax 错误请求 400 Rails

标签 javascript jquery ruby-on-rails ajax bad-request

我在使用 Ajax on Rails 时收到错误请求错误 400。 当我提交我的表单时,我有一个字符串作为参数从 Jquery 发送,我想从 params[:assignee] 中检索它,这样我就可以提取字符串并通过我的 Controller 保存它。 我的 Controller :

    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;
}

我的根目录是“task/”,您可以在其中看到保存的任务和创建新任务的表单。 我在网上搜索了很多,我都试过了。我能怎么做?非常感谢

最佳答案

400 Bad Request - The server cannot or will not process the request due to an apparent client error (e.g., malformed request syntax, too large size, invalid request message framing, or deceptive request routing).

wiki

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} 您还应该添加一个有效的 header ,'Content-Type' 和(X-CSRF-TOKEN 可选)

关于javascript - Ajax 错误请求 400 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40740796/

相关文章:

javascript - jQuery 动画无法在左侧位置工作

javascript - 无法在ajax中执行成功函数

ruby-on-rails - Rails ActiveSupport::TestCase - 彩色输出

javascript - 数组中最高和最低的问题

javascript - Highcharts 所选部分的饼图列表

Javascript 添加两个整数变量?

javascript - JQuery Remove 在页面刷新后返回?

ruby-on-rails - 具有多态性的 Rails MTI

ruby-on-rails - 如何在 linux 中轻松接收邮件并将其提供给 Rails?

javascript - Highchart 与 html 网站中 SQL 数据的范围选择器