Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

jQuery Ajax

How do I know if Ajax is setup on my server?  If it is not, is there a good tutorial that shows you how to on a windows 2008 machine?
AJAXjQuery

Avatar of undefined
Last Comment
Robert Granlund

8/22/2022 - Mon
Russ Suter

AJAX doesn't require any special server setup. It simply relies on existing HTTP GET/POST methods and runs them asynchronously through Javascript calls rather than page requests. Just call the web method using the $.ajax() function.
Robert Granlund

ASKER
I'm a newbie.  What is wrong with the way I have the following setup?
<html>
<!DOCTYPE >
<html>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<body>


	<a href="file/ajax.txt">Load AJAX File</a>
	<br /><br />
	
	<a href="file/ajax.txt">Load AJAX HTML</a>
	
	<script src="js/main.js" ></script>
</body>
</html>
</html>

<script>
(function() {
	var link = document.getElementsByTagName("a")[0];
	
	link.onClick = function() {
//  XHR Object
		var xhr = new XMLHttpRequest();
		
//  Handle the On Read State Change Event
	//  xnr. readyState property values
	// 0 = ininitialized
	// 1 = Loading
	// 2 = Loaded
	// 3 = Interactive
	// 4 = Complete
	
		xhr.onreadystatechange = function() {
			if ((xhr.readyState == 4) && (xhr.status == 200 || xhr.status == 304)) {			
				var body = document.getElementByTagName("body")[0];
				var p = document.createElement("p");
				var pText = document.createTextNode(xhr.responseText);
				p.appendChild(pText);
				body.appendChild(p);
			}
		};
	
		
//  Open request
		xhr.open("GET", "files/ajax.text", true);
		
//  Send Request to Server
		xhr.send(null);
		
		return false;
	};

})();

</script>

Open in new window

When I click the link it goes to the page instead of GETing the info and appending it.
Russ Suter

I didn't bother to inspect your code very closely once I realized that you're importing the jQuery library but not using it. jQuery makes AJAX life so much simpler.

What does your server-side method look like? I don't even see where you're referencing the server-side method.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Robert Granlund

ASKER
I don't even see where you're referencing the server-side method.?
Not using the jQuery Library?

My  "js/main.js is:
(function() {
	var link = document.getElementsByTagName("a")[0];
	
	link.onClick = function() {
//  XHR Object
		var xhr = new XMLHttpRequest();
		
//  Handle the On Read State Change Event
	//  xnr. readyState property values
	// 0 = ininitialized
	// 1 = Loading
	// 2 = Loaded
	// 3 = Interactive
	// 4 = Complete
	
		xhr.onreadystatechange = function() {
			if ((xhr.readyState == 4) && (xhr.status == 200 || xhr.status == 304)) {			
				var body = document.getElementByTagName("body")[0];
				var p = document.createElement("p");
				var pText = document.createTextNode(xhr.responseText);
				p.appendChild(pText);
				body.appendChild(p);
			}
		};
	
		
//  Open request
		xhr.open("GET", "files/ajax.txt", true);
		
//  Send Request to Server
		xhr.send(null);
		
		return false;
	};

})();

Open in new window

ASKER CERTIFIED SOLUTION
Russ Suter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Robert Granlund

ASKER
When I do:
 $.ajax({
    url : "file/ajax.txt",
    dataType: "text",
    success : ajaxCallback
    });
 
 
function ajaxCallback(result, status, xhr) {
	var body = document.getElementByTagName("body")[0];
	var p = document.createElement("p");
	var pText = document.createTextNode(result.responseText);
	p.appendChild(pText);
	body.appendChild(p);
	};

Open in new window


I get the error:
TypeError: document.getElementByTagName is not a function
      

var body = document.getElementByTagName("body")[0];
Russ Suter

Yep. I copied and pasted that from your code in the original post. That should be document.getElementsByTagName("body")[0].

It's getElementsByTagName (plural)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Robert Granlund

ASKER
Thanks.  I did google that and fix it.  However, when I click on my link in my html it still goes to the txt page instead of staying on the index.html page.
Russ Suter

I assume you're clicking on the <a> element. It will follow the href unless you add a "return false;" in the onclick event handler like this:

<a href="file/ajax.txt" onclick="return false;">Load AJAX File</a>

Open in new window


You may want to add a call to the javascript method containing the AJAX call before the return false.
Russ Suter

also another minor goof on my part. Since the dataType specified is "text" you need to modify the ajaxCallback method a little.

var pText = document.createTextNode(result.responseText);

should read

var pText = document.createTextNode(result);
Your help has saved me hundreds of hours of internet surfing.
fblack61
Russ Suter

This example in its entirety works:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript">
        function makeCall()
        {
            $.ajax({
                url: "AjaxTextFile.txt",
                dataType: "text",
                success: ajaxCallback
            });
        }

        function ajaxCallback(result, status, xhr)
        {
            var body = document.getElementsByTagName("body")[0];
            var p = document.createElement("p");
            var pText = document.createTextNode(result);
            p.appendChild(pText);
            body.appendChild(p);
        }
    </script>
</head>
<body>
    <a href="file/ajax.txt" onclick="makeCall(); return false;">Load AJAX File</a>
    <br /><br />

    <a href="file/ajax.txt">Load AJAX HTML</a>
</body>
</html>

Open in new window


You'll need to modify your "url" parameter to point to the correct file.
Robert Granlund

ASKER
Awesome!  Thanks.