SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX problem, responseText only partially full

    Hi all,
    I have an odd problem with ajax. I have been using dojo for awhile but have decided to make my own ajax function. Here is the code

    Code:
      //begins an ajax request
      function ajaxRequest(requestObj){
        var xhr = getXHR();            
        if (xhr.overrideMimeType) {
            xhr.overrideMimeType("text/javascript");
        }
        xhr.onreadystatechange = function(){processXHRResponse(xhr,requestObj)};
        xhr.open('GET', requestObj.url + Math.random(), requestObj.async);
        xhr.send(null);
      }  
    
    
      //This method is called by the response
      function processXHRResponse(xhr,requestObj){
        //readyState  0 = Uninitialized 1 = Loading  2 = Loaded  3 = Interactive  4 = completed  
         try {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    equestObj.load(eval('' + xhr.responseText));
                } else {
                    rrequestObj.error();
                }
            }
        }
        catch(e) {
            alert('Caught Exception: ' + e.description);
        }
      }
      function getXHR(){
        var xhr;
    	try{
    		// Opera 8.0+, Firefox, Safari
    		xhr = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			xhr = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				xhr = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Sorry, it looks like your browser may not be AJAX enabled");
    				return false;
    			}
    		}
    	}
        return xhr;
      }

    So, this code works just fine on most browsers, but on mozilla 1.7.12 the responseText is only partially full. I am returning large amounts of data, about 5-10 thousand characters. On mozilla, the responseText will only be partially filled a random amount.

    I tested my application with dojo and yui, it worked fine on mozilla, so what am I doing wrong??

    Thanks guys
    AFrieze

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what requestObj is doing but a simple test would be to alert the length of the returned text before you invoke the .load method:
    Code:
    alert(xhr.responseText.length); // if this fails use next line
    //alert(String(xhr.responseText).length);
    requestObj.load(eval('' + xhr.responseText));
    If the same length gets displayed in both browsers for the same request then you are getting all of the data and either eval or requestObj.load is acting funky. Otherwise, back to the drawing board.

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks jimfraser
    Thats one of the first things I tried Lengths are definitely different.
    AFrieze

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, I have printed the responseText to a div, the text just ends, randomly.

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bump
    Still boggled by this problem.
    AFrieze

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something to try:
    Code:
        setOnReadyStateChange(xhr, requestObj);
        //xhr.onreadystatechange = function(){processXHRResponse(xhr,requestObj)};
    
    ....
    
    	function setOnReadyStateChange(theXHR, theRequestObj) {
    		theXHR.onreadystatechange = function () {
    			processXHRResponse(theXHR, theRequestObj);
    		}
    	}
    Just to eliminate closures as a possibility. Other than that, not too sure =(


Bookmarks

Posting Permissions

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