Glenn.Gentzke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-May-07 16:34 UTC
Inserting authenticity token into AJAX request params
Hi all. I am trying to find a way to insert an authenticity_token
into the params of an Ajax request before it gets sent.
I''ve got this much:
Ajax.Responders.register({
onLoading: function(req){
if ((req.options.method == ''post'') && !
Object.keys(req.options.parameters).include(''authenticity_token'')){
---->
req.options.parameters.set(''authenticity_token'',
rails_gen_token());
}
alert(''method is: '' + req.options.method +
''\nparams are: '' +
Object.keys(req.options.parameters));
},
onComplete: function(){
alert(''a request completed'');
}
});
Where you see the arrow is where my problem (obviously) arises.
rails_gen_token() is a function that correctly returns a valid auth
token. What I don''t know is 1. How do I actual modify the parameters
and 2. How can i ensure this happens PRIOR to sending the request?
onLoading doesn''t seem to catch it early enough (and isn''t
guaranteed
to anyway).
Any and all help appreciated!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
Not sure if it''s the best way, but I''ve used the following to
set
parameters...
Object.extend(Ajax.Base.prototype, {
ajaxBaseSetOptions: Ajax.Base.prototype.setOptions,
setOptions: function(options){
this.ajaxBaseSetOptions(options);
this.options.parameters = this.options.parameters || {};
this.options.parameters.noCache = new Date().getTime();
this.options.parameters[''_tz_''] = "GMT-" +
(new
Date().getTimezoneOffset() / 60)
this.options.onFailure = this.options.onFailure ||
goToErrorPage;
}
});
On May 7, 11:34 am, Glenn.Gent...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
wrote:> Hi all. I am trying to find a way to insert an authenticity_token
> into the params of an Ajax request before it gets sent.
> I''ve got this much:
>
> Ajax.Responders.register({
> onLoading: function(req){
> if ((req.options.method == ''post'') && !
>
Object.keys(req.options.parameters).include(''authenticity_token'')){
> ---->
req.options.parameters.set(''authenticity_token'',
> rails_gen_token());
> }
> alert(''method is: '' + req.options.method +
''\nparams are: '' +
> Object.keys(req.options.parameters));
> },
> onComplete: function(){
> alert(''a request completed'');
> }
> });
>
> Where you see the arrow is where my problem (obviously) arises.
> rails_gen_token() is a function that correctly returns a valid auth
> token. What I don''t know is 1. How do I actual modify the
parameters
> and 2. How can i ensure this happens PRIOR to sending the request?
> onLoading doesn''t seem to catch it early enough (and
isn''t guaranteed
> to anyway).
>
> Any and all help appreciated!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
Ok, like everything else that I touch lately, 1.6 breaks my example.
Try this if you''re using 1.6+.
Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap(
function(p, options){
p(options);
this.options.parameters = this.options.parameters || {};
this.options.parameters.authenticity_token = ''token'';
}
);
On May 7, 2:53 pm, darrin
<darrin.ho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Not sure if it''s the best way, but I''ve used the
following to set
> parameters...
>
> Object.extend(Ajax.Base.prototype, {
> ajaxBaseSetOptions: Ajax.Base.prototype.setOptions,
>
> setOptions: function(options){
> this.ajaxBaseSetOptions(options);
> this.options.parameters = this.options.parameters || {};
> this.options.parameters.noCache = new Date().getTime();
> this.options.parameters[''_tz_''] =
"GMT-" + (new
> Date().getTimezoneOffset() / 60)
> this.options.onFailure = this.options.onFailure ||
> goToErrorPage;
> }
>
> });
>
> On May 7, 11:34 am, Glenn.Gent...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
wrote:
>
> > Hi all. I am trying to find a way to insert an authenticity_token
> > into the params of an Ajax request before it gets sent.
> > I''ve got this much:
>
> > Ajax.Responders.register({
> > onLoading: function(req){
> > if ((req.options.method == ''post'') &&
!
> >
Object.keys(req.options.parameters).include(''authenticity_token'')){
> > ---->
req.options.parameters.set(''authenticity_token'',
> > rails_gen_token());
> > }
> > alert(''method is: '' + req.options.method +
''\nparams are: '' +
> > Object.keys(req.options.parameters));
> > },
> > onComplete: function(){
> > alert(''a request completed'');
> > }
> > });
>
> > Where you see the arrow is where my problem (obviously) arises.
> > rails_gen_token() is a function that correctly returns a valid auth
> > token. What I don''t know is 1. How do I actual modify the
parameters
> > and 2. How can i ensure this happens PRIOR to sending the request?
> > onLoading doesn''t seem to catch it early enough (and
isn''t guaranteed
> > to anyway).
>
> > Any and all help appreciated!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---