Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I append data to a jquery-ujs post request in Rails?
    text
    copied!<p>I have an Ajax form with a <code>data-remote="true"</code> attribute which I'm submitting to a controller in rails.</p> <p>What I'd like to do is to use the <a href="https://github.com/rails/jquery-ujs" rel="noreferrer">jquery-ujs</a> <a href="https://github.com/rails/jquery-ujs/wiki/ajax" rel="noreferrer">event system</a> to append data to the request before it gets sent to the server.</p> <p>Something like this:</p> <pre><code>$("#my_form").bind('ajax:beforeSend', function(xhr, settings){ // serialize some object and append it }); </code></pre> <p>I can't figure out how to actually tag on the data though?</p> <p><strong>Edit</strong> This is what the 'xhr' object looks like in the console.</p> <pre><code>f.Event currentTarget: DOMWindow data: undefined exclusive: undefined handleObj: Object handler: function N(a){var b,c,d,e,g,h,i,j,k,l,m,n,o,p=[],q=[],r=f._data(this,"events");if(!(a.liveFired===this||!r||!r.live||a.target.disabled||a.button&amp;&amp;a.type==="click")){a.namespace&amp;&amp;(n=new RegExp("(^|\\.)"+a.namespace.split(".").join("\\.(?:.*\\.)?")+"(\\.|$)")),a.liveFired=this;var s=r.live.slice(0);for(i=0;i&lt;s.length;i++)g=s[i],g.origType.replace(y,"")===a.type?q.push(g.selector):s.splice(i--,1);e=f(a.target).closest(q,a.currentTarget);for(j=0,k=e.length;j&lt;k;j++){m=e[j];for(i=0;i&lt;s.length;i++){g=s[i];if(m.selector===g.selector&amp;&amp;(!n||n.test(g.namespace))&amp;&amp;!m.elem.disabled){h=m.elem,d=null;if(g.preType==="mouseenter"||g.preType==="mouseleave")a.type=g.preType,d=f(a.relatedTarget).closest(g.selector)[0],d&amp;&amp;f.contains(h,d)&amp;&amp;(d=h);(!d||d!==h)&amp;&amp;p.push({elem:h,handleObj:g,level:m.level})}}}for(j=0,k=p.length;j&lt;k;j++){e=p[j];if(c&amp;&amp;e.level&gt;c)break;a.currentTarget=e.elem,a.data=e.handleObj.data,a.handleObj=e.handleObj,o=e.handleObj.origHandler.apply(e.elem,arguments);if(o===!1||a.isPropagationStopped()){c=e.level,o===!1&amp;&amp;(b=!1);if(a.isImmediatePropagationStopped())break}}return b}} jQuery16103879226385615766: true liveFired: HTMLDocument namespace: "" namespace_re: /(^|\.)(\.|$)/ result: undefined target: HTMLFormElement timeStamp: 1306788242911 type: "ajax:beforeSend" __proto__: Object </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload