无法通过 Rails 中的 Ajax 请求传递参数
Unable to pass params via Ajax request in Rails
我一直在绞尽脑汁试图弄清楚我在这里做错了什么 - 我正在尝试发送带有一些数据的 ajax 请求,然后打开一个模态表单:
var position = $(".exercise-list").sortable('toArray');
var positionData = "["+position+"]"
$.ajax({
url: window.location.href + '/create_workout_routine',
method: 'POST',
data: {
position: positionData
},
success: (function() {
return false
}),
error: (function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
return false
})
});
$('#createWorkoutModal').modal('show');
当我尝试控制台.log(位置数据);我得到
[4,2]
这正是我所需要的。与我的日志相同:
Started POST "/workouts/new/create_workout_routine" for 127.0.0.1 at 2014-12-10 18:52:11 -0600
Processing by WorkoutsController#new as */*
Parameters: {"position"=>"[4,2]"}
这是我的控制器:
def create
exercise_order = params[:position]
@workout = Workout.new(workout_params)
WorkoutRoutine.create(
exercise_order.each_with_index.map { |x,i|
{:exercise_id => x,
....
}
}
)
...
end
当我的createWorkoutModal弹出时,我提交了表单,并收到此错误:
NoMethodError (undefined method `each_with_index' for nil:NilClass):
app/controllers/workouts_controller.rb:29:in `create'
我还尝试在 ajax 请求的成功功能中包含模态弹出窗口,但没有运气,以及摆脱"返回假"部分。我很困惑为什么当我看到参数被传递时它给我"零",任何帮助都非常感谢!
替换控制器中的以下行
exercise_order = params[:position]
有了这个
exercise_order = params["position"]
相关文章:
- 原型Ajax请求参数为嵌套json
- 通过Javascript传递请求参数
- 需要解释Javascript regex请求参数解析
- JSP--如何获取请求参数
- 如何自定义数据::confirm弹出窗口以提示输入,该输入将附加到请求参数中
- ExtJS使用控制器中的View Form Data作为Ajax请求参数
- 在express应用程序中,请求参数两次附加到URL
- 如何使用动态请求参数实现像谷歌一样的自动完成
- 验证请求参数的最佳方法是帆.js/节点.js
- 使用请求参数从 JavaScript 调用 servlet
- 如何获取删除请求参数
- 在 SailsJS 中验证请求参数
- 在双引号中附加请求参数
- 数据表后请求参数不起作用
- 在 node.js 中获取动态请求参数
- 有必要限制node.js中GET请求参数字符串的大小吗
- 路径参数或请求参数-从客户端角度
- 将会话ID/密钥作为请求参数传递时存在安全风险
- 访问Sails中的JSON请求参数
- 在通过RequestDispatcher调用的html页面上获取请求参数