Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag 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?
Avatar of Russ Suter
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.
Avatar of 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.
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.
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
Avatar of Russ Suter
Russ Suter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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];
Yep. I copied and pasted that from your code in the original post. That should be document.getElementsByTagName("body")[0].

It's getElementsByTagName (plural)
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.
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.
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);
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.
Awesome!  Thanks.