我正在尝试向我的 rails 控制器(中的make_suggestion
方法items controller
)发送 AJAX 请求。
我有以下 javascript:
$.ajax({
url: "/items/make_suggestion", // pass to make_suggestions in recipes controller
type: "POST",
dataType: 'json',
data: parsed_data // a Javascript object/data hash
});
启用 debug(params) 后,我在 params 中看不到任何 parsed_data。
谁能指出什么是错的?以及如何通过触发 AJAX 在 Rails 的参数中创建符号?
更新
在我的控制器中,我有
def make_suggestion
respond_to do |format|
format.html
format.json { render :json => params }
end
end
我的ajax代码:
$.ajax({
url: "http://localhost:3000/items/make_suggestion", // I'm doing the proper routing later, since '/make_suggestion' routes to 'items/1/make_suggestion'
type: "POST",
dataType: 'json',
data: parsed_data,
success: function(returned_value){
if(returned_value)
alert('true');
$('.test').append(returned_value);
},
error: function(returned_value){
$('.test').html('<p>Error in AJAX request</p>');
alert('Not working');
}
});
我的 parsed_data 看起来像这样:
参数:{"data1"=>{"position"=>"1", "item_id"=>"item_1", "to_do"=>"unknown", "selected"=>"false"}, "data2"= >{"position"=>"2", "item_id"=>"item_2", "to_do" =>"unknown", "selected"=>"false"}, "data3"=>{"position"=> "3", "item_id"=>"item_3", "to_do"=>"unknown", "selected"=>"false"}, "data4"=>{"position"=>"4", "item_id" =>"item_4", "to_do"=>"unknown", "selected"=>"false"}, "data5"=>{"position"=>"5" , "item_id"=>"item_5", "to_do"=>"unknown", "selected"=>"false"}}
但是当我去 /items/make_suggestion.json 时,它只显示以下内容:
{“动作”:“make_suggestion”,“控制器”:“食谱”,“格式”:“json”}