/    Sign up×
Community /Pin to ProfileBookmark

jQuery Ajax Problem

I am very new to jQuery so need a bit of advice. I would like to call an ajax function to search a db for usernames to see if the entered value is taken. If the entered value is then the php page returns ‘invalid’ and if it isnt it returns ‘valid’.

The problem I ahev having is that I cannot parse data from the ajax result without setting it to synchronous. I do not want to do this because it freezes the browser if you type something in. This is an example of what my code looks like. What can I do to get rid of the ‘async: false’? My first idea was use a timeout so that it does not process on each key down, it only processes if there is more than one second between each key entered. But this is not a great solution.

[code=html]function validateElement(el, v){
clearTimeout(timeout);
var returnValue;
timeout = setTimeout(function(){ $.ajax({
url: “scripts/validate.php”,
type: “GET”,
data: “el=”+el+”&v=”+v,
dataType: “html”,
cache: false,
ifModified: true,
async: false, // I want to get rid of this!!!!
success: function(data){
if(data==’valid’){returnValue=true;}
else if(data==’invalid’){returnValue=false;}
else{alert(‘error’)}
}
})}, 1000);
return returnValue;
}[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscNov 24.2010 — And why "can't" you parse the data is async is set to true?

I know what problem you're having. Your calling a function and using it's return value to dictate what you do - but there's another way to do it: do whatever it is you needed to do right in your success function.
Copy linkTweet thisAlerts:
@Benji6996authorNov 24.2010 — If I take out the 'async: false' the 'returnValue' is not parsed into my validateElement() function.

Does that make sense?
Copy linkTweet thisAlerts:
@Benji6996authorNov 24.2010 — I cannot do it in my success function because the result of the validateElement function determines something else.

e.g.

[code=html]if(validateElement()){do this}[/code]
Copy linkTweet thisAlerts:
@aj_nscNov 24.2010 — I know, trust me I know and I'm telling you the only way to do this asynchronously is to do whatever you need to do in your success function, end of story. Few things are so cut and dried in programming, but this one is.

You can't get a returnValue before the request is finished. The only way to do that is to a) either wait until the function is finished before returning a value (which is what happens in a synchronous call) or b) use your success function to perform the action you want performed.

EDIT: To try and let you know I'm on your side here and not just some know-it-all trying to shove their logic down your throat, let me explain what's happening to you:

Your code looks like this:
<i>
</i>function someFunc() {
var returnValue = '';
$.ajax(url,params,function(data) { returnValue = data; });
return returnValue;
}


When the ajax request is set to synchronous - it signals Javascript to stop performing all other actions until the request is finished - so your function will do this:

1) set returnValue to an empty string

2) perform an ajax request

3) wait for it to finish

4) still waiting, not finished yet

5) it's finished - proceed with running the callback which sets returnValue = data

6) return returnValue

If it's set to async, it does this:

1) set returnValue to an empty string

2) perform an ajax request

3) while the ajax request is being run, finish running the function - i.e. return returnValue (which is still equal to nothing)

4) still performing the request while the empty string has already been returned

5) finished the request, run the callback and set a global returnValue variable to data - even though at this point it means nothing because your function has already finished running while it was waiting for your ajax to finish.

?
Copy linkTweet thisAlerts:
@Benji6996authorNov 24.2010 — Yup okay, I hear you.

I will see what I can come up with and if I have any troubles, I will come back. Thanks for your help
×

Success!

Help @Benji6996 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...