Jump to content

AJAX Custom error handling...not catching


JakeTheSnake3.0

Recommended Posts

For some reason I'm constantly getting the error "exception thrown and not caught"...I know what it means, but I can't seem to get this code to work for me.  What I've done is that I've make a separate "AJAX" function that does the sending/receiving and passes the return string to a function which was passed as an argument....anyways, you'll see....

 

--- ajax.js ---

function AJAX(externalScript, scriptSend, notInitialized, requestSetUp, requestSent, requestInProcess, requestComplete) {

var error = new Object;
error.numReference = 0;
error.message = "";

// Create XMLHttpRequest object ----------------------------------------------------------------------------------
var xmlHttp;
try {
	// Firefox, Opera 8.0+, Safari
	xmlHttp = new XMLHttpRequest();
} catch (e) {
	// Internet Explorer
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			xmlHttp = false;
			error.numReference = 1;
			error.message = 'AJAX is not supported on your browser';
			throw(error);
		}
	}
}
// --------------------------------------------------------------------------------------------------------------------------

if (xmlHttp != false) {
	// Send data to external script -----------------------------------------------------------------------------
	if (externalScript != "") {
		xmlHttp.open("GET", externalScript, true);
		if (scriptSend != "") {
			xmlHttp.send(scriptSend);
		} else {
			try {
				xmlHttp.send(null);
			} catch(e) {
				alert(e);
			}
		}
	}
	// ------------------------------------------------------------------------------------------------------------------

	// State change function -------------------------------------------------------------------------------------
	xmlHttp.onreadystatechange = function() {
		executeIt = function(func) {
			if (typeof(func) == "function") {
				if (xmlHttp.responseText) {
					func(xmlHttp.responseText);
				} else {
					func();
				}
			}
		}

		try {
			if (xmlHttp.readyState==4) {
				if (xmlHttp.status == 200) {
					executeIt(requestComplete);
				} else {
					error.numReference = xmlHttp.status;
					error.message = 'AJAX: External page load failed';
					throw(error);
				}
			}
			if (xmlHttp.readyState==3) {
				executeIt(requestInProcess);
			}
			if (xmlHttp.readyState==2) {
				executeIt(requestSent);
			}
			if (xmlHttp.readyState==1) {
				executeIt(requestSetUp);
			}
			if (xmlHttp.readyState==0) {
				executeIt(notInitialized);
			}
		} catch(e) {
			throw(e);
		}
	}
	// ------------------------------------------------------------------------------------------------------------------
}
}

 

--- regular html file ---

<html>
<head>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>

<script type="text/javascript">
<!-- Hide from old browsers
// <![CDATA[
alertMe = function(returnVar) {
	alert(returnVar);
}

tryAJAX = function() {
	try {
		// for this purpose, you DO NOT need testAjax2.php - this is because I'm trying to generate a custom error
		AJAX('testAjax2.php','','','','','',alertMe);
	} catch(e) {
		//document.write(e.message);
		alert(e);
		// "e" is supposed to be an object which was thrown from the AJAX() function...
	}
}
// ]]>
// Stop hiding from old browsers -->
</script>

<form name="myForm">
Input: <input type="text" onkeydown="tryAJAX();" name="username" />
</form>

</body>
</html>

 

Any help would be greatly appreciated....in Firefox, it tells me that the error is occurring at the "xmlHttp.send(null);" line....which makes sense, because the error is a 404 - not found error....but for some reason that error isn't being caught by my try...catch statements...

Link to comment
https://forums.phpfreaks.com/topic/41397-ajax-custom-error-handlingnot-catching/
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.