3 Replies - 50348 Views - Last Post: 12 August 2010 - 01:47 AM

#1 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

want to wait until ajax response comes

Post icon  Posted 15 November 2008 - 12:20 AM

Hi,
i am using following code
function callajax()
{
	var handleSubmit = function() //this function is related to yahooYUI.so i want this should be like this.
	{
		var temp=do_login();
		alert(temp);
	};
}

function do_login()
{
				var retValue=false;
		AjaxRequest.submit
		(
			fobj,//fobj is a reference to my form. don't worry abt it
			{
				"onSuccess":function(req)
				{
					var retval = CB_do_login(req);
					retValue=ret;
					//alert(ret+"\n"+temp);					
				},
				"onerror":function(req){ CB_error_do_login(req);},
				"onTimeout":function(req){ CB_timeout_do_login(req);}
			}
		);

return retValue;
}



now what i want to do is
i have a callajax() function. with in this function i will call do_login() function. this do_login() handles an ajaxrequest and returns the responsetext.

now i want to do some validation on this responsetext(in case of onsuccess).
so i am trying to return value to callajax() function for onSuccess case in ajaxrequest.submit();

that is(onsuccess response) supposed to be some string( but not true or false).
but i am always getting false in ajaxcall() function.
i know the do_login() function is returning false before ajaxrequest completes

so i want to stop this and make do_login wait until ajaxrequest completes and then i want to return it's response to callajax() function

can anyone help me

This post has been edited by kummu4help: 17 November 2008 - 05:21 AM


Is This A Good Question/Topic? 0
  • +

#13 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4401
  • View blog
  • Posts: 12,257
  • Joined: 18-April 07

Re: want to wait until ajax response comes

Posted 16 November 2008 - 10:27 AM

In ajax there are two methods for calling code, asynchronous (which is the default for most scripts and typically what you want) where it will continue processing a script while the call is being made or synchronous which will stop the script until the ajax call is completed.

The difference between the two is a parameter passed during the XMLHttpRequest object's open method. Instead of something like XMLHttpRequest.open("GET", url); you will add another parameter which is "false" like XMLHttpRequest.open("GET", url, false);.

This tells the javascript to wait and continue after receiving the ajax call routine's response from the server.

The danger in this is that if the server never returns data you could potentially freeze the entire script. Plus while this is happening the user interface that this ajax is powering may also stop responding until the server can be contacted. This is why most advise to stay away from the synchronous calls, but if done right it can be exactly what you need.

To mitigate the problem with it freezing indefinitely, you can create a timeout where if the response doesn't come the script will timeout and then continue on its way.

Below is one site that shows you how this is done and talks a bit more about synchronous ajax calls and provides snippets.

Snippets: Synchronous AJAX

Enjoy!

"At DIC we be synchronous code ninjas... we kill but only after we have had afternoon tea." :snap:
Was This Post Helpful? 2

#14 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: want to wait until ajax response comes

Posted 17 November 2008 - 09:31 PM

Thanks martyr2,

at present i am using ajaxrequest.js library. so can u give me some suggestion on how to wait till ajax request completes while using ajaxrequest.js

u suggested me in general. but i need specific help regarding ajaxrequest library.

because we r using that library in our project.

so can u tell me how to make function wait till response comes
Was This Post Helpful? 0
  • +
  • -

#15 Guest_kapil*


Reputation:

Re: want to wait until ajax response comes

Posted 12 August 2010 - 01:47 AM

View Postkummu4help, on 17 November 2008 - 08:31 PM, said:

Thanks martyr2,

at present i am using ajaxrequest.js library. so can u give me some suggestion on how to wait till ajax request completes while using ajaxrequest.js

u suggested me in general. but i need specific help regarding ajaxrequest library.

because we r using that library in our project.

so can u tell me how to make function wait till response comes



many thanks Martyr2

i found very helpfull to me thanks great help for me

you are a life saver

thanks
kapil
Was This Post Helpful? 0

Page 1 of 1