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?
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterCommented:
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.
0
rgranlundAuthor Commented:
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.
0
Russ SuterCommented:
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.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

rgranlundAuthor Commented:
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

0
Russ SuterCommented:
That's not jQuery. A jQuery AJAX call would look something like this:

  $.ajax({
    url : "files/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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor Commented:
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];
0
Russ SuterCommented:
Yep. I copied and pasted that from your code in the original post. That should be document.getElementsByTagName("body")[0].

It's getElementsByTagName (plural)
0
rgranlundAuthor Commented:
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.
0
Russ SuterCommented:
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.
0
Russ SuterCommented:
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);
0
Russ SuterCommented:
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.
0
rgranlundAuthor Commented:
Awesome!  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.