troubleshooting Question

Sample Ajax request

Avatar of JMRFan4Life
JMRFan4Life asked on
JavaScriptScripting LanguagesAJAX
7 Comments1 Solution560 ViewsLast Modified:
hey all,

i am trying to write a plain javascript example that makes a simple ajax request and i cannot get it to work. Not sure what I am missing, the Jquery method works.

I have created a folder with this code below in an html file and a plain text file called test.txt

Thanks
<!DOCTYPE html>  
<html>  
<head>
<title>Ajax test</title> 
 
</head>  
<body>

<div id="container">fail blog</div>
<script type="text/javascript">

		var xhr;
		
		xhr = new XMLHttpRequest();

		xhr.onreadystatechange = ensureReadiness;

		function ensureReadiness() {
			if(xhr.readyState < 4) {
				return;
			}

			if(xhr.status !== 200) {
				return;
			}

			// all is well
			alert('wtf');
			console.log(xhr.responseText);
			
		}

		xhr.open('GET', 'test.txt', true);
		xhr.send('');
		
		//jquery method
		//$('#container').load('test.txt');
</script>
</body>
</html>
ASKER CERTIFIED SOLUTION
experts1

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros