943,169 Members | Top Members by Rank

Ad:
Aug 31st, 2010
0

JQYERY AJAX Callback function value

Expand Post »
Hi,
I have a $.ajax call in my jquery function.
I want to access the value returned from the server(php) to be accessed in jquery function. Hoe to do this??
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. setTimeout(function(){
  2. $.ajax({
  3. url: 'ajax-validation.php',
  4. data: 'email=' + $('#email').val(),
  5. dataType:'text',
  6. type: 'post',
  7. success: function (resp) {
  8. if(resp=="Email is available")/*This does not seems to work*/
  9. return true;
  10. else
  11. return false; /*False is returned always*/
  12. emailInfo.text(resp);//This works. The text is shown in html page...
  13. }
  14. });},800);
  15. t.lastValue = t.value;
  16. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hari.sarvothama is offline Offline
15 posts
since Aug 2010
Sep 3rd, 2010
0
Re: JQYERY AJAX Callback function value
If the result being returned from the server has leading and/or trailing blank spaces, then the if condition will always evaluate to false. Try using if( resp.indexOf("Email is available") >-1) instead.
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007
Sep 4th, 2010
0

Change In code

Hey hielo thanks for replying..but i changed the code i'm still havin problem
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. setTimeout(function(){
  2. $.ajax({
  3. url: 'ajax-validation.php',
  4. data: 'email=' + $('#email').val(),
  5. dataType:'text',
  6. type: 'post',
  7. success: function (resp) {
  8. if(resp=='true')
  9. {
  10. t.lastValue = t.value;
  11. emailInfo.text("Email Id is available").addClass("valid");//This works
  12. x=true;//This assignment does not seem to work
  13. }
  14. else
  15. {
  16. t.lastValue = t.value;
  17. emailInfo.text("Email Id is in use").addClass("error");//This works
  18. x=false;//This assignment does not seem to work
  19. }
  20. }
  21. });},800);
  22. }
  23. }
  24.  
  25. return x;//This always returns the value to which it was initialized i.e false
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hari.sarvothama is offline Offline
15 posts
since Aug 2010
Sep 4th, 2010
0
Re: JQYERY AJAX Callback function value
That's because when you make an ajax call, by default you are making an asynchronous call. To clarify, given this:
javascript Syntax (Toggle Plain Text)
  1. alert("hello");
  2. callSomeFunction();//let's say this function takes 5 seconds to execute
  3. alert( "goodbye" );

normally/traditionally, you would first see "hello", then wait for 5 seconds before you see "goodbye". That is NOT what happens with asynchronous ajax calls. You would see "hello", the the function is called, but the javascript interpreter does NOT wait for it to finish. Instead it continues immediately to the next statement and executes it. So the reason you are not seeing the returned values is that by the time the ajax request completes, the other statements after you initial ajax call have executed!


So your follow up question now is "So how do I get the returned value?". That's what the callback function is for.

So if you were originally wanting to do something like:
javascript Syntax (Toggle Plain Text)
  1. var x = myAjaxCall();
  2. $('#someElement').text( x );

now you will need to execute that statement within your callback function instead. Why? because the callback function executes after the asynchronous call is complete.
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007
Sep 5th, 2010
0
Re: JQYERY AJAX Callback function value
Now i understand the problem. But based on the string returned to callback i want to change the return value of function from which the ajax callback function is called. My question now is if i give return statement within the callback as you suggested would not that become the return value of callback function rather than that of the javascript function??? Is there any way to make javascript interpreter to wait for ajax call to complete(i.e make d call sync.)?
Hope my questions are clear....
Last edited by hari.sarvothama; Sep 5th, 2010 at 2:23 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hari.sarvothama is offline Offline
15 posts
since Aug 2010
Sep 5th, 2010
0
Re: JQYERY AJAX Callback function value
Quote ...
Is there any way to make javascript interpreter to wait for ajax call
Yes, just provide the async:false option to the ajax call:
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007
Sep 5th, 2010
0
Re: JQYERY AJAX Callback function value
Your codes are fine but I have a question though, what is the response type of your php code? is it in a json format? or just simply text like (echo "this is a text")

I think you shouldn't make your ajax to async: false because this will prevent the user from interacting in your page if the response time of your server is slow.
Last edited by lambing; Sep 5th, 2010 at 10:43 pm.
Reputation Points: 10
Solved Threads: 5
Light Poster
lambing is offline Offline
30 posts
since Feb 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Dynamic dropdown
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: page completely loaded





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC