Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31360 > unrolled thread

What's supposed to be the correct syntax to call function that has jQuery ajax within?

Started byjustaguy <lichunshen84@gmail.com>
First post2016-09-13 13:00 -0700
Last post2016-09-16 18:12 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  What's supposed to be the correct syntax to call function that has jQuery ajax within? justaguy <lichunshen84@gmail.com> - 2016-09-13 13:00 -0700
    Re: What's supposed to be the correct syntax to call function that has jQuery ajax within? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-14 00:25 +0100
      Re: What's supposed to be the correct syntax to call function that has jQuery ajax within? justaguy <lichunshen84@gmail.com> - 2016-09-16 18:13 -0700
    Re: What's supposed to be the correct syntax to call function that has jQuery ajax within? JJ <jj4public@vfemail.net> - 2016-09-14 06:52 +0700
      Re: What's supposed to be the correct syntax to call function that has jQuery ajax within? justaguy <lichunshen84@gmail.com> - 2016-09-16 18:12 -0700

#31360 — What's supposed to be the correct syntax to call function that has jQuery ajax within?

Fromjustaguy <lichunshen84@gmail.com>
Date2016-09-13 13:00 -0700
SubjectWhat's supposed to be the correct syntax to call function that has jQuery ajax within?
Message-ID<61ffa1ca-36ca-4081-a1f8-b7e51966bb50@googlegroups.com>
Hi,

I attempted to call a function by assign it to a variable's value but it complained about "undefined", what is the correct way?  Thanks.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>

// ajax call for a server side script with State and County parameters
function ckCounty(st,cunty) {
	$.ajax({
		url: 'whateverScript.file?state='+st+'&county='+cunty,
		success: function(rtn) {
			rtn = eval(rtn);
			if (rtn > 0) 
			{ 
				return 'green';		
			}	
		 // setTimeout(ckCounty, 2000);    
		}
	});
}


var	Color = 'grey';
Color = ckCounty('ga','TAYLOR');

console.log(Color);

</script>

[toc] | [next] | [standalone]


#31364

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-09-14 00:25 +0100
Message-ID<874m5jpekk.fsf@bsb.me.uk>
In reply to#31360
justaguy <lichunshen84@gmail.com> writes:

> I attempted to call a function by assign it to a variable's value but
> it complained about "undefined", what is the correct way?  Thanks.
>
> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
> <script>
>
> // ajax call for a server side script with State and County parameters
> function ckCounty(st,cunty) {
> 	$.ajax({
> 		url: 'whateverScript.file?state='+st+'&county='+cunty,
> 		success: function(rtn) {
> 			rtn = eval(rtn);
> 			if (rtn > 0) 
> 			{ 
> 				return 'green';		
> 			}	
> 		 // setTimeout(ckCounty, 2000);    
> 		}
> 	});
> }
>
>
> var	Color = 'grey';
> Color = ckCounty('ga','TAYLOR');
>
> console.log(Color);
>
> </script>

This code exhibits a number of misunderstandings.  It's probably best to
take things one step at a time.  The line

  Color = ckCounty('ga','TAYLOR');

assigns to Colour the value returned from the function call
ckCounty('ga','TAYLOR').  But that call does not return a value
explicitly so Color gets the value 'undefined'.  What value do you think
Color should have?  'green'?  If so, you need to back up and learn a bit
about how asynchronous requests work (that's the "A" in AJAX).  By the
time the server has handled the request and the success function gets
called, the ckCounty function has long since finished and returned
whatever value it was going to.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#31389

Fromjustaguy <lichunshen84@gmail.com>
Date2016-09-16 18:13 -0700
Message-ID<c8bad695-3f59-45b1-8522-80ac88ccc898@googlegroups.com>
In reply to#31364
You're right.  Thanks.

On Tuesday, September 13, 2016 at 7:25:51 PM UTC-4, Ben Bacarisse wrote:
> justaguy <.com> writes:
> 
> > I attempted to call a function by assign it to a variable's value but
> > it complained about "undefined", what is the correct way?  Thanks.
> >
> > <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
> > <script>
> >
> > // ajax call for a server side script with State and County parameters
> > function ckCounty(st,cunty) {
> > 	$.ajax({
> > 		url: 'whateverScript.file?state='+st+'&county='+cunty,
> > 		success: function(rtn) {
> > 			rtn = eval(rtn);
> > 			if (rtn > 0) 
> > 			{ 
> > 				return 'green';		
> > 			}	
> > 		 // setTimeout(ckCounty, 2000);    
> > 		}
> > 	});
> > }
> >
> >
> > var	Color = 'grey';
> > Color = ckCounty('ga','TAYLOR');
> >
> > console.log(Color);
> >
> > </script>
> 
> This code exhibits a number of misunderstandings.  It's probably best to
> take things one step at a time.  The line
> 
>   Color = ckCounty('ga','TAYLOR');
> 
> assigns to Colour the value returned from the function call
> ckCounty('ga','TAYLOR').  But that call does not return a value
> explicitly so Color gets the value 'undefined'.  What value do you think
> Color should have?  'green'?  If so, you need to back up and learn a bit
> about how asynchronous requests work (that's the "A" in AJAX).  By the
> time the server has handled the request and the success function gets
> called, the ckCounty function has long since finished and returned
> whatever value it was going to.
> 
> -- 
> Ben.

[toc] | [prev] | [next] | [standalone]


#31366

FromJJ <jj4public@vfemail.net>
Date2016-09-14 06:52 +0700
Message-ID<1le0sc20btu78.9fed3lbjmtgr.dlg@40tude.net>
In reply to#31360
On Tue, 13 Sep 2016 13:00:16 -0700 (PDT), justaguy wrote:
> Hi,
> 
> I attempted to call a function by assign it to a variable's value but it complained about "undefined", what is the correct way?  Thanks.
> 
> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
> <script>
> 
> // ajax call for a server side script with State and County parameters
> function ckCounty(st,cunty) {
> 	$.ajax({
> 		url: 'whateverScript.file?state='+st+'&county='+cunty,
> 		success: function(rtn) {
> 			rtn = eval(rtn);
> 			if (rtn > 0) 
> 			{ 
> 				return 'green';		
> 			}	
> 		 // setTimeout(ckCounty, 2000);    
> 		}
> 	});
> }
> 
> var	Color = 'grey';
> Color = ckCounty('ga','TAYLOR');
> 
> console.log(Color);
> 
> </script>

You're assigning `Color` variable with the return value of `ckCounty()`
function. That function doesn't return any value, so its return value
defaults to `undefined`, which is correct.

If you want to assign the `Color` variable to the ajax result, assign it in
the ajax's `success` handler. e.g.

  rtn = eval(rtn);
  if (rtn > 0) 
  { 
    Color = 'green';
  } else
    Color = 'red';
  }

[toc] | [prev] | [next] | [standalone]


#31388

Fromjustaguy <lichunshen84@gmail.com>
Date2016-09-16 18:12 -0700
Message-ID<3f446df4-1467-4fa1-b297-bc261de48a4a@googlegroups.com>
In reply to#31366
Yeah, thanks, resolved.

On Tuesday, September 13, 2016 at 7:52:54 PM UTC-4, JJ wrote:
> On Tue, 13 Sep 2016 13:00:16 -0700 (PDT), justaguy wrote:
> > Hi,
> > 
> > I attempted to call a function by assign it to a variable's value but it complained about "undefined", what is the correct way?  Thanks.
> > 
> > <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
> > <script>
> > 
> > // ajax call for a server side script with State and County parameters
> > function ckCounty(st,cunty) {
> > 	$.ajax({
> > 		url: 'whateverScript.file?state='+st+'&county='+cunty,
> > 		success: function(rtn) {
> > 			rtn = eval(rtn);
> > 			if (rtn > 0) 
> > 			{ 
> > 				return 'green';		
> > 			}	
> > 		 // setTimeout(ckCounty, 2000);    
> > 		}
> > 	});
> > }
> > 
> > var	Color = 'grey';
> > Color = ckCounty('ga','TAYLOR');
> > 
> > console.log(Color);
> > 
> > </script>
> 
> You're assigning `Color` variable with the return value of `ckCounty()`
> function. That function doesn't return any value, so its return value
> defaults to `undefined`, which is correct.
> 
> If you want to assign the `Color` variable to the ajax result, assign it in
> the ajax's `success` handler. e.g.
> 
>   rtn = eval(rtn);
>   if (rtn > 0) 
>   { 
>     Color = 'green';
>   } else
>     Color = 'red';
>   }

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web