[jQuery] Re: Ajax - Access data arguments in callback

2009-09-18 Thread Peter Edwards


Hi there,

The only way I could find to do this with the $.get function was to 
parse out the query string - the success callback should get a copy of 
the options object which was used to set up the ajax request, but the 
data parameter is NULL in this object for GET requests (it is populated 
by the data parameters in POST requests however).


For example (using your example with a few changes):

$(function(){
 $('input:checkbox').click( function() {
   $.get('/ajax.php',{'nr': $(this).attr("id") }, function( data, 
textStatus ) {
 // this contains the options object used in the ajax request - 
this.data is null in GET requests

 // parse out the query string used for the request
 var qs = this.url.substr((this.url.indexOf("?")+1));
 var nvp = qs.split("&");
 // make an object which will hold the key/value pairs
 var params = {};
 for (var i = 0; i < nvp.length; i++) {
   var p = nvp[i].split("=");
   params[p[0]] = p[1];
 }
 $('#'+params.nr).attr("checked", "checked");
   });
   return false;
 });
});








on 18/09/2009 08:53 Flamer said::

Thanks for your reply.

Does this not gives problems when I have an AJAX request with a
timeout after say 2 seconds, but in the meantime there's another
request using the same variable? Or is the sentData only accessible in
the click( function(){ } ) scope?

There is no other way of getting the data I sent with the request?

On 17 sep, 15:28, Nick Fitzsimons  wrote:
  

2009/9/17 Flamer :







Hello,
  
Is it possible to access the data arguments that are sent with the

ajax function?
  
Example:

$('#mycheckbox').click( function() {
   $.get( '/ajax.php', { nr: this.id }, function( data, textStatus ) {
   // Here I want to access the arguments I just sent with ajax.php
  
   // As example, something like this:

   $('#'+arguments.nr).attr('checked', 'checked');
   } );
  
   return false;

} );
  

If I understand you correctly, then assigning your data object to a
variable which can be accessed via a closure in the callback function
would do it - something like:

$('#mycheckbox').click( function() {
   var sentData = { nr: this.id };
   $.get( '/ajax.php', sentData, function( data, textStatus ) {
   // Here I want to access the arguments I just sent with ajax.php

   // As example, something like this:
   $('#'+ sentData.nr).attr('checked', 'checked');
   } );

   return false;

} );

should be all you need.

Regards,

Nick.
--
Nick Fitzsimonshttp://www.nickfitz.co.uk/



  


[jQuery] Re: Ajax - Access data arguments in callback

2009-09-18 Thread Flamer

Thanks for your reply.

Does this not gives problems when I have an AJAX request with a
timeout after say 2 seconds, but in the meantime there's another
request using the same variable? Or is the sentData only accessible in
the click( function(){ } ) scope?

There is no other way of getting the data I sent with the request?

On 17 sep, 15:28, Nick Fitzsimons  wrote:
> 2009/9/17 Flamer :
>
>
>
>
>
> > Hello,
>
> > Is it possible to access the data arguments that are sent with the
> > ajax function?
>
> > Example:
> > $('#mycheckbox').click( function() {
> >        $.get( '/ajax.php', { nr: this.id }, function( data, textStatus ) {
> >                // Here I want to access the arguments I just sent with 
> > ajax.php
>
> >                // As example, something like this:
> >                $('#'+arguments.nr).attr('checked', 'checked');
> >        } );
>
> >        return false;
> > } );
>
> If I understand you correctly, then assigning your data object to a
> variable which can be accessed via a closure in the callback function
> would do it - something like:
>
> $('#mycheckbox').click( function() {
>        var sentData = { nr: this.id };
>        $.get( '/ajax.php', sentData, function( data, textStatus ) {
>                // Here I want to access the arguments I just sent with 
> ajax.php
>
>                // As example, something like this:
>                $('#'+ sentData.nr).attr('checked', 'checked');
>        } );
>
>        return false;
>
> } );
>
> should be all you need.
>
> Regards,
>
> Nick.
> --
> Nick Fitzsimonshttp://www.nickfitz.co.uk/


[jQuery] Re: Ajax - Access data arguments in callback

2009-09-17 Thread Nick Fitzsimons

2009/9/17 Flamer :
>
> Hello,
>
> Is it possible to access the data arguments that are sent with the
> ajax function?
>
> Example:
> $('#mycheckbox').click( function() {
>        $.get( '/ajax.php', { nr: this.id }, function( data, textStatus ) {
>                // Here I want to access the arguments I just sent with 
> ajax.php
>
>                // As example, something like this:
>                $('#'+arguments.nr).attr('checked', 'checked');
>        } );
>
>        return false;
> } );
>

If I understand you correctly, then assigning your data object to a
variable which can be accessed via a closure in the callback function
would do it - something like:

$('#mycheckbox').click( function() {
   var sentData = { nr: this.id };
   $.get( '/ajax.php', sentData, function( data, textStatus ) {
   // Here I want to access the arguments I just sent with ajax.php

   // As example, something like this:
   $('#'+ sentData.nr).attr('checked', 'checked');
   } );

   return false;
} );

should be all you need.

Regards,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/