I do this on two of my plugins, flXHR proxy and mpAjax. These were the
two I talked about in my jQuery Conf talk on extending Ajax, and it
comes down to exactly this method, wrapping the $.ajax call.

But, one thing I would suggest, do this instead:

(function($){
   var _ajax = $.ajax;

   $.extend({
      ajax:function(o){
         if (this.data......
         ....
         return _ajax.call(this,o);
      }
   });
})(jQuery);

I believe that's the more suggested way of extending/overriding
functions the "jQuery" way. Also, the ".call()" makes sure to preserve
the "this" binding for the original ajax call, which I think is a
safer way to deal with jQuery internals overriding.




On Sep 23, 1:52 am, gMinuses <gminu...@gmail.com> wrote:
> Thank you very much! Great idea!
>
> Here is my implementation, works so far:
>
> (function ($) {
>
>     var _ajax = $.ajax,
>     A = $.ajax = function(options) {
>         if (A.data)
>                         if(options.data) {
>                                 if(typeof options.data !== 'string')
>                                         options.data = $.param(options.data);
>
>                                 if(typeof A.data !== 'string')
>                                         A.data = $.param(A.data);
>
>                                 options.data += '&' + A.data;
>                         } else
>                                 options.data = A.data;
>
>         return _ajax(options);
>     };
>
> })(jQuery);
>
> On Sep 22, 10:57 pm, Nathan Bubna <nbu...@gmail.com> wrote:
>
>
>
> > My JSON-REST plugin does this by wrapping $.ajax with other method(s).
> >  Just add an object of params to 
> > $.rest.data.http://plugins.jquery.com/project/rest
>
> > You can do this by wrapping/replacing the $.ajax method yourself
>
> > (function ($) {
>
> >     var _ajax = $.ajax,
> >         A = $.ajax = function(options) {
> >             if (A.data) {
> >                 //TODO: the appending in here, i just don't feel like
> > writing all of this
> >             }
> >             return _ajax(options);
> >         };
> >     };
>
> > })(jQuery);
>
> > // then make a call this:
> > $.ajax.data = { myParam: 'alwaysAppendThis' };
> > // and all your ajax calls will get that data
> > $.get({...})
> > $.post({...})
> > // and so on
>
> > On Tue, Sep 22, 2009 at 1:18 AM, gMinuses <gminu...@gmail.com> wrote:
>
> > > Is there a way to automatically append some parameters in every ajax
> > > call's query string?
>
> > > "ajaxStart" event doesn't send ajax options to callback function, so
> > > there is not way to modify the query string.
>
> > > "ajaxSend" event doesn't work with GET request, because the query
> > > string is already appended to the url and xhr is already open, so
> > > there is no way to change the url.
>
> > > For "beforeSend", it's the same as "ajaxSend".
>
> > > Set global setting in ajaxSetup will not work, if "data" is specified
> > > in individual ajax calls.
>
> > > I'd like to add parameters like 'mode=ajax' to make server respond as
> > > little data as posible(e.g, no header, no footer), and degrade
> > > gracefully.
>
> > > I have many ajax calls to make, and adding these parameters to every
> > > single of them manually is very tedious.
>
> > > So how can I achieve this in jQuery?
>
> > > Thanks.- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to