www.webdeveloper.com
Results 1 to 4 of 4

Thread: callback function gets called twice

Threaded View

  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

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