www.webdeveloper.com
Results 1 to 4 of 4

Thread: callback function gets called twice

  1. #1
    Join Date
    Sep 2009
    Posts
    51

    callback function gets called twice

    So I am getting some data via AJAX, from a Java servlet on the server side.

    I had some alert boxes set up, and the request gets submitted only once.
    But, the callBack function in the onreadystatechange iterates through the states twice - reaches state == 4 , status == 200, does its work, and then goes through the states again... I have no idea why this is happening?

    Here is the code:

    Code:
    function createRequest()
    {
    	try
    	{
    		request = new XMLHttpRequest();
    	}
    	catch(tryMS)
    	{
    		try
    		{
    			request = new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch(otherMS)
    		{
    			try
    			{
    				request = new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch(failed)
    			{
    				request = null;
    			}
    		}
    	}
    	
    	return request;
    }
    Code:
    function getSubmitRequest()
    {
    
    	//Create request object
    	req1 = createRequest();
    	
    	if(req1 == null)
    	{
    		alert("Unable to create request");
    		return;
    	}
    	
    	//Configure request object
    	var url = "/ajaxurl";
    	
    	req1.open("POST",url,true);
    	
    	//Tell the request object what to do when server responds -assign a call-back function
    	req1.onreadystatechange = populatePage;
    	
    	//Make the request
    	req1.send(null);
    
    }


    Code:
    function populatePage()
    {
    
    	
    	if(req1.readyState == 4)
    	{
    		if(req1.status == 200)
    		{			
    			var responseDoc = req1.responseXML;
    			populatePageParts(responseDoc);
    		}
    	}
    }
    Last edited by romsok; 12-17-2009 at 02:38 PM. Reason: spelling mistakes

  2. #2
    Join Date
    Jan 2005
    Posts
    347
    I can't see anything wrong with the bits you have given us, but clearly this is an extract of the whole page, so maybe the fault lies elsewhere?
    Have you tried debugging this - first thing I would want to know is if the function is being called twice, or if it is looping somehow, and to double-check whether the callback was somehow getting registered twice.

  3. #3
    Join Date
    Sep 2009
    Posts
    51

    Resolved

    This stopped happening after I removed some alert boxes.

  4. #4
    Join Date
    Jan 2005
    Posts
    347
    If I am working on anything significant, I usually set up a DIV that I can use as a logging console - there is almost nothing that is as frustrating as putting an alert within an onfocus method (or something similar) and having it loop endlessly as a result...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles