We help IT Professionals succeed at work.

jQuery AJAX - Cant Get A return on A Function Call.

waf771
waf771 asked
on
3,148 Views
Last Modified: 2012-05-05
I am trying to return a date value via AJAX through jQuery but cannot get the value to return out of the 'success' function.
I have tried the following ways but to no avail.  I can write the results to HTML or a text box but cannot get it to return to the original submitting function.
I will be here for a couple of hours tonight then back in the morning, Ive been working on this all day so any help is appriciated. Thanks

////////////////////////Try 1/////////////////////////////
function DayEvents(Date1)
{

var t=(Date1);

$.ajax({
   type: "POST",
   url: "CalendarUI-asp.asp?action=K",
   data: "Z="+ t,
   success: ProcDay
 });
}

function ProcDay(responseText)
{      
$("input[@name='Proj1']").setValue([responseText]);       //this works fine
return(responseText);      // this wont return a value to my original function      
}

////////////////////////Try 2/////////////////////////////
function DayEvents(Date1)
{
      
  var t= (Date1);

 $.post("CalendarUI-asp.asp?action=K",{Z: t},function(txt){
   alert(txt); //This works fine
   $("#CalBox").html(txt); //This works fine
   return(txt); //This wont return a value to my original function
 });
}
////////////////////////Try 3/////////////////////////////

function DayEvents(Date1)
{
  var Date2 = ("0");
  var t= (Date1);

 $.post("CalendarUI-asp.asp?action=K",{Z: t},function(txt){
   $("#CalBox").html(txt); //This works fine
   Date2 = (txt); //no errors
 });
}

return(Date2); //This only returns "0" no date
Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>>// this wont return a value to my original function      
That's because when you call DayEvents, it submits the ajax request but does NOT wait for the result from the server. That's how asynchronous request are meant to work. So when you call DayEvents, the ajax request is sent and DayEvents returns immediately, even if the ajax request takes 10 secs to complete. The whole point of using the success call back is for you to call some other function after the request has completed successfully. If you want it to wait, you need to send a synchronous request, but this has the side-effect of "freezing" your browser until the request completes. That's why this type of  request is seldom used.

Author

Commented:
What is the best way to call sql data in the middle of  javascript code then. I need to pull data in and process it. is there annother way besides AJAX???
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.