[英]Bad Request, bad URI 400 Error
I'm 'ajaxing' my rails web app something I haven't done before. 我正在“整理”我的Rails Web应用程序,这是我以前从未做过的事情。 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.
但是我遇到了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); }'.
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: 用户控制器的show方法:
def show
@user = User.find(params[:id])
@product = Product.new
end
The jQuery function that has the ajax functionality: 具有ajax功能的jQuery函数:
$('.edit-box > form input[type=submit]').click(function(e) {
closeEdit();
event.preventDefault();
$.ajax({
url: Routes.products_path,
dataType: 'json',
type: 'POST'
})
});
Any ideas? 有任何想法吗?
Routes.product_path
is a javascript function so you need to call it with brackets. Routes.product_path
是一个javascript函数,因此您需要使用方括号将其调用。 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. 如果不调用它,则将功能代码作为ajax调用的url,这就是为什么会收到BAD URI错误的原因。
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. 无论如何,您应该设置一些要发送的参数,否则,如果您对模型进行了一些验证,则控制器将创建一个空产品(如果允许),否则将返回500错误 。
To send params use the data
option. 要发送参数,请使用
data
选项。 (Documentation here: http://api.jquery.com/jQuery.ajax ) (此处的文档: 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", ... }
})
});
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. 要获取值,您可以在表单上使用
serializeArray()
jQuery方法,但是您将获得一个对象数组,其中包含表单输入的name
和value
。 If you want a plain object, you can easly add the serializeObject()
method to jQuery. 如果您想要一个普通对象,则可以轻松地将
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.