Jump to content

Ajax - no reponseText


watagal

Recommended Posts

Greetings-I appriciate all your help! I'm diving in to AJAX now and can not (yet) get a reponse from my server (local).My JS code:

// create AJAX objectvar xmlHttp = null;function createAjaxObject() {	try {		xmlHttp=new XMLHttpRequest();}		// Firefox, Opera 8.0+, Safari	catch (e) {		try {	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}	// Internet Explorer		catch (e) {	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}	}	return xmlHttp;}// AJAX callfunction ajaxCall(phpFile) {	xmlHttp = createAjaxObject();	if (xmlHttp == null) {		alert ("Browser does not support HTTP Request");		return;	}		xmlHttp.onreadystatechange = function() {alert (xmlHttp.readyState);		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {alert (xmlHttp.responseText);			return xmlHttp.responseText;		}	}	xmlHttp.open("GET", phpFile+"&sid="+Math.random(), true);	xmlHttp.send(null);}function validateSignIn(pageId) {	var userName = document.frmSignIn.txtUserName.value.toLowerCase();	var password = document.frmSignIn.txtPassword.value;		var validation = ajaxCall('./php_code/validateSignIn.php?u='+userName+'&p='+password');	if (validation.substr(0,18) == "Invalid User Name:") {		document.getElementById('divInvalidUserName').style.display = "block";			} else if (validation.substr(0,17) == "Invalid Password:") {		document.getElementById('divInvalidPassword').style.display = "block";			} else if (validation == "Successful") {		window.location="index.php?p="+pageId;			} else {		document.write(validation);	}}

My PHP reponse (./php_code/validateSignIn.php) is:

<?php	echo "Successful";?>

In my JS function "ajaxCall()", the alert(xmlHttp.readyState) is called twice displaying "1" - never reaching 2,3,4 before terminating. Additionally, the 'validation' variable is never defined in JS function "validateSignIn()" because "document.write(validation)" is always executing - reporting "undefined".What am I doing wrong?TiA, Gal

Link to comment
Share on other sites

Your problem is probably the document.write. Try assigning validation to a div through the DOM or innerHTML. Also you if else statements are not waiting for your ajax call to complete (that's what asyncronous means).You can either change it to a syncronous call

xmlHttp.open("GET", phpFile+"&sid="+Math.random(), false);

or assign a callback handler like this.

// AJAX callfunction ajaxCall(phpFile, callback) {	xmlHttp = createAjaxObject();	if (xmlHttp == null) {		alert ("Browser does not support HTTP Request");		return;	}		xmlHttp.onreadystatechange = function() {		alert (xmlHttp.readyState);		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {			if(xmlHttp.status == 200) {				if(callback) { callback(xmlHttp.responseText); }			}		}	}	xmlHttp.open("GET", phpFile+"&sid="+Math.random(), true);	xmlHttp.send(null);}function callbackHandler(responseText) {	var validation = responseText;	if (validation.substr(0,18) == "Invalid User Name:") {		document.getElementById('divInvalidUserName').style.display = "block";			} else if (validation.substr(0,17) == "Invalid Password:") {		document.getElementById('divInvalidPassword').style.display = "block";			} else if (validation == "Successful") {		window.location="index.php?p="+pageId;			} else {		 document.getElementById('divFailedValidation').innerHTML(validation);	}}ajaxCall('./php_code/validateSignIn.php?u='+userName+'&p='+password', callbackHandler);

Link to comment
Share on other sites

Gracias aspnetguy!

You can either change it to a syncronous call
xmlHttp.open("GET", phpFile+"&sid="+Math.random(), false);

I changed my call to syncronous as per above. Now, my alert (xmlHttp.responseText); statement displays what looks like the HTML code for a 404 page. So I assume it's not finding my php file or is it and I'm still clueless on this ajax stuff.How can I verify I am reading the PHP file? Also, I have kept the PHP file simple, only echo-ing a single string. Does the PHP file need more structure? Header info? etc.?TiA, Gal
Link to comment
Share on other sites

It doesn't need anything special. If you install Firebug for Firefox you can use that to look at the request going out and the response from the server to see what's happening. If it's sending a 404 back you'll be able to see that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...