Hello I have a form that submits remotely with the jQuery UJS for rails. I binded to the beforeSend event to allow me to modify the data submitting to the serve. It’s not working. Here is what I have in the beforeSend:
settings.data = JSON.stringify({
'list_item[title]' : 'hi?? there'
})
This doesn’t work. In the server logs I see this:
Started POST "/lists/9/list_items" for 127.0.0.1 at 2011-10-24 14:04:36 -0700
Processing by ListItemsController#create as JSON
Parameters: {"{\"list_item"=>{"title"=>{"\":\"hi?? there\"}"=>nil}}, "list_id"=>"9"}
Any idea what I’m doing wrong? I want to customize the settings.data with added fields that aren’t in the form. Thanks
You don’t need to stringify anything to put it in
settings.data. Thedatais:What you’re doing is putting this string:
into
databut that string is not a query string so things are going to get confused. You should be able to simply assign your JavaScript object tosettings.data:and let jQuery sort it out from there.
Update based on evidence rather than documentation:
However, further investigation reveals that this doesn’t work. If I send a GET request, any changes I make to
settings.dataare ignored but if I send a POST request, then changes tosettings.datastick but you have to use the query string format to get anything sensible through:The version of
settings.datacombined with a POST request gets me this:on the server and that looks like what you’re after. If you want to preserve some of the original parameters then you’ll have to unpack and repack the query string by hand.