Jump to content

simple AJAX script


fugix

Recommended Posts

Hey guys, im starting to learn ajax and im trying an example that is not working for me..here is my script..

<span id='ajaxButton' style='cursor:pointer; text-decoration: underline'>Make a Request</span>

<script type='text/javascript'>

(function()  {
var httpRequest;
document.getElementById("ajaxButton").onclick = function() { makeRequest('test.html'); };

function makeRequest(url)  {
	if (window.XMLHttpRequest)  {
		httpRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject)  {
	try  {
		httpRequest = new ActiveXObject("Msxml2,XMLHTTP");
	      }
	catch (e)  {
		try   {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} 
	catch (e)  {}
		}
	}

	if (!httpRequest)  {

		alert('Giving up  cannot create an XMLHTTP instance');
		return false;
	}
	httpRequest.onreadystatechange = alertContents;
	httpRequest.open('GET', url);
	httpRequest.send();
	}

	function alertContents()  {

		if (httpRequest.readyState === 4)  {
			if(httpRequest.status === 20)  {
				alert(httpRequest.responseText);
	} else  {

		alert('There was a problem with the request');
	}
}
}
}
)();

</script>

 

I keep getting the alert "alert('There was a problem with the request');" as my result as opposed to the response text....if anyone can spot my error i would appreciate it

 

Link to comment
https://forums.phpfreaks.com/topic/236711-simple-ajax-script/
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.