AJAX hates me all the way to hell

Discussion in 'OT Technology' started by babygodzilla, Jan 17, 2008.

  1. babygodzilla

    babygodzilla I love rice

    Joined:
    Nov 5, 2001
    Messages:
    3,108
    Likes Received:
    0
    i dunno if im just dumb or what. i can code javascript, but for some reason AJAX always seems to fail me. performance is always spotty. can someone much smarter than me tell me what I am doing wrong here? I have an app to upload images. The form is inside a JSP, and I am using AJAX to exchange messages between a Servlet and the JSP itself in order to print out error messages without reloading the whole page. let it be noted that this code always works whenever I put breakpoints in Firebug. otherwise, its very spotty. the AJAX calls the Servlet using a GET method. without further ado, here's the AJAX, and the corresponding doGet() method.

    First, AJAX script:

    Code:
    var req;
    
    function ajaxFunction()
    {
        var url = "Upload";
    
        if (window.XMLHttpRequest) // Non-IE browsers
        { 
            req = new XMLHttpRequest();
            
            try 
            {
                req.open("GET", url, true);
            } 
            catch (e) 
            {
                alert(e);
            }
            req.send(null);
            req.onReadyStateChange = processStateChange();
        } 
        else if (window.ActiveXObject) // IE Browsers
        { 
            req = new ActiveXObject("Microsoft.XMLHTTP");
    
            if (req) 
            {
                req.open("GET", url, true);
                req.send();
                req.onReadyStateChange = processStateChange();
            }
        }
    }
    
    
    function processStateChange()
    {
        /**
        *  State    Description
        *    0      The request is not initialized
        *    1      The request has been set up
        *    2      The request has been sent
        *    3      The request is in process
        *    4      The request is complete
        */
        
        if (req.readyState == 4)
        {
            if (req.status == 200) // OK response
            {
                var xml = req.responseXML;
      
                // No need to iterate since there will only be one set of lines
                var isNotFinished = xml.getElementsByTagName("finished")[0];
                var myStatus = xml.getElementsByTagName("status")[0];
    
                // Check to see if it's even started yet
                if ((isNotFinished == null))
                {
                    window.setTimeout("ajaxFunction();", 100);
                }
                else 
                {	
                	if (myStatus.firstChild.data == "100") {
                		document.getElementById("message").innerHTML = "File upload finished!";
                	} else if (myStatus.firstChild.data == "101") {
                		document.getElementById("message").innerHTML = "File size too large. Please choose a file less than 100 KB and try again.";
                	}
                }
            }
            else
            {
                document.getElementById("message").innerHTML = "We're sorry, an error has occured. Please try again. Status: " + req.statusText;
            }
        }
        
    }
    Now, doGet() method:
    Code:
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		System.out.println("Ajax called me");
    		// TODO Auto-generated method stub
    		PrintWriter out = response.getWriter();
    		HttpSession session = request.getSession();
    		FileUploadListener listener = null;
    		StringBuffer buffy = new StringBuffer();
    		int status = 0;
    
    		// Make sure the session has started
    		if (session == null)
    		{
    			return;
    		}
    		else if (session != null)
    		{
    			// Check to see if we've created the listener object yet
    			listener = (FileUploadListener)session.getAttribute("LISTENER");
    
    			if (listener == null)
    			{
    				return;
    			}
    			else
    			{
    				status = listener.getStatus();
    			}
    		}
    
    		response.setContentType("text/xml");
    
    		buffy.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
    		buffy.append("<response>\n");
    		buffy.append("\t<status>" + status + "</status>\n");
    
    		// Check to see if we're done
    		if (status == FileUploadListener.OK || status == FileUploadListener.SIZE_LIMIT) 
    		{
    			buffy.append("\t<finished />\n");
    
    			// No reason to keep listener in session since we're done
    			session.setAttribute("LISTENER", null);
    		}
    
    		buffy.append("</response>\n");
    
    		out.println(buffy.toString());
    		out.flush();
    		out.close();
    
    	}
    obviously i havent posted every line of code i have, that'll be too long. so if there's anything in there that you don't understand, ill gladly post more code to clarify. however, these two pieces of code are the gist of the problem and i just cant figure out what's going on... i will :bowdown: to anyone that can help.

    THANKS!!
     
  2. ez4me2c3d

    ez4me2c3d Cold Member

    Joined:
    Sep 19, 2003
    Messages:
    1,836
    Likes Received:
    0
    Location:
    Minneapolis, MN
    and by :bowdown: do you men :paypal: ?
     
  3. ez4me2c3d

    ez4me2c3d Cold Member

    Joined:
    Sep 19, 2003
    Messages:
    1,836
    Likes Received:
    0
    Location:
    Minneapolis, MN
    in addition... why are you not using a framework of some sort?

    take a look at some ajax code here, that periodically updates an element. So compact and easy to use.
    http://www.avholloway.com/sandbox/woot/
     
  4. 95vr4

    95vr4 OT Supporter

    Joined:
    Oct 6, 2004
    Messages:
    2,513
    Likes Received:
    0
    Location:
    Weddington, NC
    or prototype, ajax is so easy with prototype, like 3 lines of code and you've got an ajax app up and running :hsd:. Combine it with scriptaculous and you've got everything you need to easily make an awesome app.

    think ez's actually the one that helped me get started on it a few weeks ago :wavey:
     

Share This Page