weixin_33682719 2013-12-04 15:44
浏览 53

错误的请求,错误的URI 400错误

I'm 'ajaxing' my rails web app something I haven't done before. My goal is to add a product to the database without refreshing the page

I have a User and Product Controller. A user in his personal page adds a product. But I'm getting an 400 Error.

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); }'.

and also

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'.

Product Controller code:

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

User Controller, the show method:

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

The jQuery function that has the ajax functionality:

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

Any ideas?

  • 写回答

1条回答 默认 最新

  • python小菜 2013-12-04 18:09
    关注

    Routes.product_path is a javascript function so you need to call it with brackets. Without calling it you are putting the function code as url of your ajax call, and that's why you get a BAD URI error.

    Try with:

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

    Anyway you should set some params to send, or your controller will create an empty product if it's allowed or will return a 500 Error if you have some validation on your model.

    To send params use the data option. (Documentation here: 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", ... }
      })
    });
    

    UPDATE

    To get the value you can use the serializeArray() jQuery method on your form, but you will get an array of object with name and value of your form inputs. If you want a plain object, you can easly add the serializeObject() method to 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()
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?