DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Responsetext is blank without alertbox

  1. #1
    Join Date
    Jan 2009
    Posts
    1

    Responsetext is blank without alertbox

    Hello Everyone,

    I am stuck in a weird AJAX problem and its been sometime I have tried fixing it. I am doing a simple AJAX call on my PHP page. Here is the Javascript that is not working:

    Code:
     var http_request = false;
    	   function makePOSTRequest(url, parameters, eid) {
    	   	 
    		  http_request = false;
    		  
    		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    			 http_request = new XMLHttpRequest();
    			 if (http_request.overrideMimeType) {
    				// set type accordingly to anticipated content type
    				//http_request.overrideMimeType('text/xml');
    				http_request.overrideMimeType('text/html');
    			 }
    		  } else if (window.ActiveXObject) { // IE
    			 try {
    				http_request = new ActiveXObject("Msxml2.XMLHTTP");
    			 } catch (e) {
    				try {
    				   http_request = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e) {}
    			 }
    		  }
    		  if (!http_request) {
    			 alert('Cannot create XMLHTTP instance');
    			 return false;
    		  }		  
    		  http_request.open('POST', url, true);
    		  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    		  http_request.setRequestHeader("Content-length", parameters.length);
    		  http_request.setRequestHeader("Connection", "close");
    		  http_request.send(parameters);
    		  http_request.onreadystatechange = alertContents(eid);
    	   }
    	
    	   function alertContents(id) {
    	   	// alert(http_request.readyState); //commented
    		  if (http_request.readyState == 4) {
    			 if (http_request.status == 200) {
    				result = http_request.responseText;
    				document.getElementById(id).innerHTML = result;     
    			 } else {
    				//alert('There was a problem with the request.');
    			 }
    		  }else{
    		  }
    	   }

    And here is the Javascript that works:
    Code:
     var http_request = false;
    	   function makePOSTRequest(url, parameters, eid) {
    	   	 
    		  http_request = false;
    		  
    		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    			 http_request = new XMLHttpRequest();
    			 if (http_request.overrideMimeType) {
    				// set type accordingly to anticipated content type
    				//http_request.overrideMimeType('text/xml');
    				http_request.overrideMimeType('text/html');
    			 }
    		  } else if (window.ActiveXObject) { // IE
    			 try {
    				http_request = new ActiveXObject("Msxml2.XMLHTTP");
    			 } catch (e) {
    				try {
    				   http_request = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e) {}
    			 }
    		  }
    		  if (!http_request) {
    			 alert('Cannot create XMLHTTP instance');
    			 return false;
    		  }		  
    		  http_request.open('POST', url, true);
    		  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    		  http_request.setRequestHeader("Content-length", parameters.length);
    		  http_request.setRequestHeader("Connection", "close");
    		  http_request.send(parameters);
    		  http_request.onreadystatechange = alertContents(eid);
    	   }
    	
    	   function alertContents(id) {
    	   	alert(http_request.readyState); //uncommented
    		  if (http_request.readyState == 4) {
    			 if (http_request.status == 200) {
    				result = http_request.responseText;
    				document.getElementById(id).innerHTML = result;     
    			 } else {
    				//alert('There was a problem with the request.');
    			 }
    		  }else{
    		  }
    	   }
    The responsetext is received properly if I put this alert(http_request.readyState); statement. Also, it shows me the value 1 which means that the object is initializing but not loaded or completed. After I click ok on the alertbox the responsetext is outputed correctly. I also checked the value of readystate after the alertbox and it shows me 4. So, this clearly means that it is taking time for the readystate to attain the completion and a value of 4.

    Surprisingly, if I try to set a delay by using setTimeout('alert(http_request.readyState)',5000) then the readystate changes to 4 but it still won't give out the right responsetext. It is blank. I even checked the value of http_request.status and it comes out to be 200. Then why the code is not working? It only works when I use alert(http_request.readyState); only as given in the above code snippet.

    Any ideas guy what is happening? Any help will be really appreciated.

    Thanks.

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    try instead of this line:
    http_request.onreadystatechange = alertContents(eid);

    this:
    Code:
    //eid is defined somewhere isn't it??
    http_request.onreadystatechange=function() {
      if (http_request.readyState == 4 && http_request.status == 200) {
    	result = http_request.responseText;
    	document.getElementById(eid).innerHTML = result;     
      }
    }
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

Similar Threads

  1. Replies: 4
    Last Post: 02-20-2010, 04:17 AM
  2. How to execute responseText?
    By pondugud in forum AJAX
    Replies: 0
    Last Post: 05-13-2008, 09:48 AM
  3. Replies: 1
    Last Post: 08-01-2006, 01:12 AM
  4. ASP blank fields
    By vero in forum ASP.NET
    Replies: 1
    Last Post: 09-27-2002, 04:37 PM
  5. Updating a bound data control with a blank.
    By Shawn Jager in forum VB Classic
    Replies: 0
    Last Post: 05-17-2000, 12:32 PM

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center