AnApprentice
AnApprentice

Reputation: 111030

How do you append data to an XHR request beforeSend?

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

Upvotes: 2

Views: 1664

Answers (2)

Kristian PD
Kristian PD

Reputation: 2695

If you're using a newer version of jquery_ujs you should be able to use the data-params (if you're able to do it at this point). Otherwise, you can bind to ajax:before and modify the data at that point. I'd learn towards data-params myself.

Here's a bit more detail to the same problem:

https://github.com/rails/jquery-ujs/issues/168

Upvotes: 1

Keith.Abramo
Keith.Abramo

Reputation: 6965

settings.data["list_item"] = { 'title' : input_val,
    'position' : 0,
    'open_positions' : 'stuff',
    'done_positions' : 'stuff'
};

try something like this. You can create the "list_item" property and set it dynamically in js like this.

Upvotes: 0

Related Questions