如何在发送之前将数据附加到 XHR 请求?
我有一个使用Rails UJS 以remote=true 提交的表单。
在 beforeSend 事件中我想附加数据。我正在尝试以下操作:
onNewListItemFormBeforeSend : function(event, xhr, settings) {
settings.data += { 'list_item[title]' : input_val,
'list_item[position]' : 0,
'list_items[open_positions]' : 'stuff',
'list_items[done_positions]' : 'stuff' )
};
}
但这是在我注销 settings.data 时添加一个对象...将这些项目附加到 settings.data 的正确方法是什么?
谢谢
更新
我也尝试这样做:
settings.data = { 'number' : 'XXXXXX' }
但这没有用。在日志中我看到:
Started POST "/lists/9/list_items" for 127.0.0.1 at 2011-10-24 11:55:39 -0700
Processing by ListItemsController#create as JSON
Parameters: {"object Object"=>nil, "list_id"=>"9"}
为什么对象对象为零?
谢谢
I have a form that submits with remote=true using Rails UJS.
In the beforeSend event I want to append data. I'm trying with the following:
onNewListItemFormBeforeSend : function(event, xhr, settings) {
settings.data += { 'list_item[title]' : input_val,
'list_item[position]' : 0,
'list_items[open_positions]' : 'stuff',
'list_items[done_positions]' : 'stuff' )
};
}
But this is adding an object when I log out settings.data ... What is the right way to append these items to settings.data?
Thanks
UPDATE
I also tried just doing:
settings.data = { 'number' : 'XXXXXX' }
But that didn't work. In the logs I see:
Started POST "/lists/9/list_items" for 127.0.0.1 at 2011-10-24 11:55:39 -0700
Processing by ListItemsController#create as JSON
Parameters: {"object Object"=>nil, "list_id"=>"9"}
Why the object object nil?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是较新版本的
jquery_ujs
,您应该能够使用data-params
(如果您此时能够执行此操作)。否则,您可以绑定到 ajax:before 并修改此时的数据。我会自己学习data-params
。以下是同一问题的更多详细信息:
https://github.com/rails/jquery -ujs/issues/168
If you're using a newer version of
jquery_ujs
you should be able to use thedata-params
(if you're able to do it at this point). Otherwise, you can bind toajax:before
and modify the data at that point. I'd learn towardsdata-params
myself.Here's a bit more detail to the same problem:
https://github.com/rails/jquery-ujs/issues/168
尝试这样的事情。您可以创建“list_item”属性并在js中动态设置它,如下所示。
try something like this. You can create the "list_item" property and set it dynamically in js like this.