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


Groups > comp.lang.javascript > #31364

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

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: What's supposed to be the correct syntax to call function that has jQuery ajax within?
Date 2016-09-14 00:25 +0100
Organization A noiseless patient Spider
Message-ID <874m5jpekk.fsf@bsb.me.uk> (permalink)
References <61ffa1ca-36ca-4081-a1f8-b7e51966bb50@googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web