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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.