Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX function returns before status is 200 1

Status
Not open for further replies.

AndyInNC

Programmer
Sep 22, 2008
76
US
I'm doing a simple database validation. To avoid duplicate names, I want to see if the First and Last names are in the database and tell the user before writing the data. So I've got an ASP page that works fine. It returns the count of records where FName like 'xxx%' and LName like 'yyy%'. Running it alone it works great, so no problems there.


[COLOR=white blue]Here's the outline:[/color]
[blue]html:[/blue]
<button type='button' onclick='savePerson();'>

[blue]JS:[/blue]
function savePerson(){
var myTemp = checkIfNameExists(FName, LName);
alert(myTemp); [highlight #F3F3F3]// returns Undefined[/highlight]

if ( myTemp == true )
//notify user
else
//add person
}

function checkIfNameExists(FName, LName){
...
xmlHttp.onReadyStateChange = function() {
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
var sTemp = xmlHttp.responseText;
[highlight #F4F4F4]// an alert with sTemp here returns a valid value[/highlight]
return (sTemp > 0) ? true : false;
}
else { return false; }
}
}
xmlHttp.open("GET", url, false);
xmlHttp.send(null);
}

[COLOR=white blue]The problem:[/color]
What happens is checkIfNameExists() keeps coming back Undefined in the savePerson() call, but it looks fine in the Ajax call if status is 200. To me, it appears that what is happening is the first xmlHttp status is not 200 so the func returns false to my calling function and shortly afterward gets the 200 success, but by then, savePerson() has already received a false.

I would appreciate any thoughts.
 
Hi

I would try to make it asynchronous :
Code:
xmlHttp.open("GET", url, [red]true[/red]);
Or maybe is just the [tt]return[/tt] expression...
Code:
return [red]parseInt([/red]sTemp[red],10)[/red] > 0

Feherke.
 
No luck.

I've tried the async approach, but I got further along (trying to) make sure one thing happened at a time -- attempting to slow it down to process everything in order.

But thanks for the suggestion.
 
I would appreciate any thoughts.

OK - Don't you think it's a bit short-sighted to assume that there are no people in this world who happen to share the same forename and surname as someone else?

My surname isn't what I'd call common, but I went to university with someone who had the same forename, middle name and surname as me... and for those with common surnames (Smith, etc), there are going to be many, many 'duplicates'.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
responseText is a string, are you trying to get its value or its length?

Cheers,
Dian
 
The ASP will return a count of found records -- always a number of 0 or more. The AJAX function should return TRUE if responseText > 0 and FALSE if == 0. I really don't care about the length.
 
Y may be wrong, but the http call will fork the code, so the function will return before the response is given (hence the undefined value).

You would need to move the code to the part where the response is processed.

Cheers,
Dian
 
I totally agree, but with my code outline above, how do I make that happen?

...maybe setTimeout?
 
LOL, BillyRay is really concerned about those people. I'd make an association :p

Back to the code

Code:
function savePerson(){
    checkIfNameExists(FName, LName);
}

function checkIfNameExists(FName, LName){
   ...
   xmlHttp.onReadyStateChange = function() {
      if (xmlHttp.readyState == 4)
            {
                if (xmlHttp.status == 200)
                    {
                        var sTemp = xmlHttp.responseText;
                        // an alert with sTemp here returns a valid value
                        if ( sTemp > 0 )
                         //notify user
                        else
                       //add person
                     }
   }
   xmlHttp.open("GET", url, false);
   xmlHttp.send(null);
}

Cheers,
Dian
 
Diancecht said:
LOL, BillyRay is really concerned about those people

No - I just hate dumb logic rules such as on the website I used the other day that told me my name was invalid because at three characters it was deemed to be 'too short'.

If I tried to sign up to a site an was told I couldn't enter my real name when asked for it, I'd be a tad pissed off. Wouldn't you?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
ShortNamedBillyRay said:
If I tried to sign up to a site an was told I couldn't enter my real name when asked for it, I'd be a tad pissed off. Wouldn't you?

No.

1.- I never use my real name
2.- If I don't sign up the website is the loser, not me

Appart from that, you're right, but maybe this is not the forum to discuss that. As a side note, I will tell you that there's nothing that you can use to uniquely identify a person.


Cheers,
Dian
 
I used to have a friend named John Smith. He had the hardest time ordering pizza for delivery. As soon as they found out what his name was, they thought it was a prank. If everybody had a unique name though (or better, an identifying number)....

jk

Actually the name thing is not a restriction, just an notification to the user that that person might already exist so please double-check before adding that person again.

As far as the coding, thanks Dian for the response. Essentially this approach will remove the need for the savePerson() function (just call checkIfNameExists() directly and rename it appropriately). I'll lose the ability of the Yes/No function return value, but I'll be a lot further on with the project at hand.

Practical and simple.

Thanks again.
 
Glad it helps.

I always found a little annoying the asynchronous paradigm, because I think that breaks the procedural strucutre we're used to follow.

So the only way I know to do things with AJAX is to realize you're losing control in your function when invoking the http request and the "program thread" will continue in another function.

Cheers,
Dian
 
Sorry to join this thread so late - but I am confused
Code:
var sTemp = xmlHttp.responseText;
if ( sTemp > 0 ) ...
From what I understand (and that could be written on the head of a pin) [red]xmlHttp.responseText[/red] is a String. So comparing a String to an integer (ie [red]sTemp > 0[/red]) isn't valid unless some variable type conversion happens.

Is [red]0[/red] being converted from an int to a String and then the compare [red]>[/red] is comparing the two strings?

What if [blue]sTemp = ""[/blue]? What will that return?

Einstein47 (Starbase47.com)
“PI is like love - simple, natural, irrational, endless, and very important“
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top