SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Rancho Cordova
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with xmlHTTPRequest Post In FF

    Here is a small example of my issue, as you can see if you run the code below, the readyState is not changing in firefox after the send(). It should change to 2, it does in IE, but I digress. I will also note that the function to check the status and perform the call back is never called when the readyState changes from 0 to 1, after the open(). What am I doing wrong, this is almost the exact same code that I use for a GET (which works fine in FF), only addition is the setting of the headers. Where am I going wrong? This is really burning up my morning.

    Code:
    <!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    	<head>
    		<title>Enegam.com</title>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    		<script type="text/javascript">
    			function getReqObjPost(url,params,func)
    			{
    				if (window.XMLHttpRequest)
    			  	{
    			 		xmlhttp=new XMLHttpRequest();
    			 	}
    			 	else if (window.ActiveXObject)
    			    {
    			    	if(new ActiveXObject("Microsoft.XMLHTTP"))
    			    	{
    			    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    			    	}
    			    	else
    			    	{
    			    		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    			    	}
    			    }
    			  	
    			  	xmlhttp.onreadyStateChange=function()
    			  	{
    			  		alert("You won't ever see this, if you do thanks, tell me what you did!");
    			  		if(xmlhttp.readyState==4)
    					{	
    						if(xmlhttp.status==200)
    						{
    							var req_str=xmlhttp.responseText;
    							var req=xmlhttp.responseXML;
    							eval(func);
    						}
    						else
    						{
    							xmlError(xmlhttp.status);	
    						}
    					}
    			  	}
    				var now=new Date();
    				params=params + "&c_date"+now.getSeconds()+"=" + now;
    				alert("Ready State: " + xmlhttp.readyState);
    				xmlhttp.open("POST",url,true);
    				alert("Ready State: " + xmlhttp.readyState);
    				xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    			    xmlhttp.setRequestHeader("Content-length", params.length);
    				xmlhttp.send(params);
    				alert("Ready State: " + xmlhttp.readyState);
    			}
    			function getStuff()
    			{
    				getReqObjPost('negam.asp','name=basscyst&lame=yes','finishTest(req)');
    			}
    			function finishTest(req)
    			{
    				alert(req.documentElement.tagName);
    			}
    		</script>
    	</head>
    	<body>
    		<input type="button" onclick="getStuff();" value="Test It" />
    	</body>
    </html>
    FYI, I also have this posted here.

    Thanks,
    Basscyst

  2. #2
    SitePoint Enthusiast Sjoerd's Avatar
    Join Date
    Jun 2005
    Location
    Leimuiden, The Netherlands
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this line insteed of the two setRequestHeader lines you already have

    Code:
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    The javascript engine will handle the Content-length header by itself after you submit the data...

  3. #3
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "onreadystatechange" should all be in lowercase

    That and the fix Sjoerd posted above should do it.

    I tried a copy of it here with some edits to track the request progress:
    http://www.digitalbonsai.com/testing.htm

    Here's the source:
    Code:
    <!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    	<head>
    		<title>Enegam.com</title>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    		<script type="text/javascript">
    			function getReqObjPost(url,params,func)
    			{
    				if (window.XMLHttpRequest)
    			  	{
    			 		xmlhttp=new XMLHttpRequest();
    			 	}
    			 	else if (window.ActiveXObject)
    			    {
    			    	if(new ActiveXObject("Microsoft.XMLHTTP"))
    			    	{
    			    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    			    	}
    			    	else
    			    	{
    			    		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    			    	}
    			    }
    			  	
    			  	xmlhttp.onreadystatechange=function()
    			  	{
    			  		document.getElementById('statusmsg').innerHTML += "You won't ever see this, if you do thanks, tell me what you did! State/Status : " + xmlhttp.readyState + "/" + xmlhttp.status + "<br />";
    			  		if(xmlhttp.readyState==4)
    					{	
    						if(xmlhttp.status==200)
    						{
    							var req_str=xmlhttp.responseText;
    							document.getElementById('results').innerHTML = req_str;
    						}
    						else
    						{
    							//xmlError(xmlhttp.status);	
    						}
    					}
    			  	}
    				var now=new Date();
    				params=params + "&c_date"+now.getSeconds()+"=" + now;
    				document.getElementById('statusmsg').innerHTML += 'Ready State: ' + xmlhttp.readyState + '<br />';
    				xmlhttp.open("POST",url,true);
    				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    				xmlhttp.send(params);
    			}
    			function getStuff()
    			{
    				getReqObjPost('/files/file-with-html.php','name=basscyst&lame=yes','finishTest(req)');
    			}
    			function finishTest(req)
    			{
    				alert(req.documentElement.tagName);
    			}
    		</script>
    	</head>
    	<body>
    		<input type="button" onclick="getStuff();" value="Test It" />
    		<div id="statusmsg"></div>
    		<div id="results"></div>
    	</body>
    </html>

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Rancho Cordova
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha! Awesome, that did the trick.

    Thanks a million guys.

    Basscyst


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
  •