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