Page 1 of 1

xmlhttprequest help!!!

Posted: Fri Sep 23, 2005 3:59 pm
by skolar
I'm using xmlhttprequest to grab info from my database and output it to the screen without having to refresh the page...a fairly standard and seemingly easy task for AJAX.

However, i always seem to be getting a null response in the xmlhttprequest object. I verified its null two ways, with xmlhttprequest debugger and by checking the actual packets with ethereal.

When i call the server script, with the same request url as passed to the xmlhttprequest object, manually, everything works fine. Its only when i call it through the object that the response is null. Does anyone have any idea whats going on?

Also, when checking ethereal, the http GET returns 200 but the data is not to be found. What happened to the output?

I'm new to AJAX and from what i've seen online, server side php scripts are just echo'ing the data to be returned to the object. Is this correct? Do i need to do something else to return the data?

Thanks in advance

Posted: Fri Sep 23, 2005 4:13 pm
by Weirdan
As you have no idea on what's going on, it would be useful to see your code (javascript part would be sufficient to start with).

Posted: Fri Sep 23, 2005 4:17 pm
by skolar

Code: Select all

<script language="JavaScript">
//XMLhttp variable will hold the XMLHttpRequest object

function preSearch() {
	var xmlhttp = false;        
	// If the user is using Mozilla/Firefox/Safari/etc
	if (window.XMLHttpRequest) {
	   //Intiate the object
  	   xmlhttp = new XMLHttpRequest();
   	   //Set the mime type
   	   //xmlhttp.overrideMimeType('text/html');
	}
	// If the user is using IE
	else if (window.ActiveXObject) {
		//Intiate the object
   		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
    //Put the form data into a variable
	var theQuery = document.getElementById('tag_id').value;
	var job_id = document.getElementById('j').value;
    //If the form data is *not* blank, query the DB and return the results
	if(theQuery !== "") {
		var url = 'http://www.karmaone.org/staging/ajax.php?tag=' + theQuery + '&j=' + job_id;
		//document.write(url);
        //Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		if(xmlhttp) {
		xmlhttp.open('GET', url, true);
		//Check that the PHP script has finished sending us the result
		xmlhttp.onreadystatechange = function() {
			//document.getElementById('tag_results').innerHTML = xmlhttp.readyState;
			if((xmlhttp.readyState == 4)&&(xmlhttp.status == 200)) {
                //Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById('tag_results').innerHTML = xmlhttp.responseText + ' ';
			} else {
                //If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				document.getElementById('tag_results').innerHTML = ' ' + xmlhttp.status + ' ' + xmlhttp.readyState;
			}
		};
		xmlhttp.send(null);
		}
		else { document.getElementById('tag_results').innerHTML = 'xmlhttp is false'; }  
	}
}
</script>

Posted: Sun Sep 25, 2005 1:05 pm
by CoderGoblin
Have you looked at Burrito's tutorial about whos online... That may give you some hints.