【问题标题】:Bad Request, bad URI 400 Error错误请求,错误 URI 400 错误
【发布时间】:2013-12-04 15:44:45
【问题描述】:

我正在“ajaxing”我的 Rails Web 应用程序,这是我以前没有做过的事情。我的目标是在不刷新页面的情况下将产品添加到数据库中

我有一个用户和产品控制器。用户在他的个人页面中添加了一个产品。但我收到 400 错误。

Bad Request

bad URI `/users/function (options) { return Utils.build_path([], ["format"],[2,[2,[7,"/",false],[6,"products",false]],[1,[2,[8,".",false],[3,"format",false]],false]], arguments); }'.

还有

bad URI `/users/function%20(options)%20%7B%20%20return%20Utils.build_path([],%20[%22format%22],%20[2,[2,[7,%22/%22,false],[6,%22products%22,false]],[1,[2,[8,%22.%22,false],[3,%22format%22,false]],false]],%20arguments);%20%20%7D'.

产品控制器代码:

class ProductsController < ApplicationController

respond_to :html, :json

    def create
        @product = Product.new(product_params)
        respond_to do |format|
            if @product.save
                format.html do
                    redirect_to '/'
                end
                format.json { render json: @product.to_json }
            else
                # Add a handler for unsuccessful cases
            end
        end

    end
    def product_params
        params.require(:product).permit(:title, :units)
    end
end

用户控制器,显示方法:

def show
    @user = User.find(params[:id])
    @product = Product.new
end

具有ajax功能的jQuery函数:

    $('.edit-box > form input[type=submit]').click(function(e) {
        closeEdit();
        event.preventDefault();
        $.ajax({
            url: Routes.products_path,
            dataType: 'json',
            type: 'POST'
        })
     });

有什么想法吗?

【问题讨论】:

  • 您是否使用可以监控网络请求的浏览器(例如带有检查器的 Chrome 或 Safari,或带有 firebug 的 Firefox)?如果是这样,该请求的响应是什么?它将为您提供有关请求有什么问题的更多详细信息。
  • 我正在使用 Chrome(我已经用新信息更新了帖子)

标签: javascript ruby ajax jquery ruby-on-rails-4


【解决方案1】:

Routes.product_path 是一个 javascript 函数,因此您需要用括号调用它。如果不调用它,您会将函数代码作为您的 ajax 调用的 url,这就是您收到 BAD URI 错误的原因。

尝试:

$('.edit-box > form input[type=submit]').click(function(e) {
    closeEdit();
    event.preventDefault();
    $.ajax({
        url: Routes.products_path(),
        dataType: 'json',
        type: 'POST'
    })
 });

无论如何,您应该设置一些要发送的参数,否则如果允许,您的控制器将创建一个空产品,或者如果您对模型进行了一些验证,则会返回 500 错误。

要发送参数,请使用data 选项。 (此处的文档:http://api.jquery.com/jQuery.ajax)

$('.edit-box > form input[type=submit]').click(function(e) {
   closeEdit();
   event.preventDefault();
   $.ajax({
      url: Routes.products_path(),
      dataType: 'json',
      type: 'POST',
      data: {key1: "value1", key2: "value2", ... }
  })
});

更新

要获取值,您可以在表单上使用serializeArray() jQuery 方法,但您将获得一个包含name 和value 表单输入的对象数组。如果你想要一个普通的对象,你可以很容易地将 serializeObject() 方法添加到 jQuery 中。 (Source)

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

/* Now you can use the method to serialize your form to an object */
var dataToSend = $('.edit-box > form').serializeObject()

【讨论】:

  • 如何引用 HTML 表单中的键和值?
猜你喜欢
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多