Robert Granlund
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?
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.
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>
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.
What does your server-side method look like? I don't even see where you're referencing the server-side method.
ASKER
I don't even see where you're referencing the server-side method.?
Not using the jQuery Library?
My "js/main.js is:
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;
};
})();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
When I do:
I get the error:
TypeError: document.getElementByTagNa me is not a function
var body = document.getElementByTagNa me("body") [0];
$.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);
};
I get the error:
TypeError: document.getElementByTagNa
var body = document.getElementByTagNa
Yep. I copied and pasted that from your code in the original post. That should be document.getElementsByTagN ame("body" )[0].
It's getElementsByTagName (plural)
It's getElementsByTagName (plural)
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.
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:
You may want to add a call to the javascript method containing the AJAX call before the return false.
<a href="file/ajax.txt" onclick="return false;">Load AJAX File</a>
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(re sult.respo nseText);
should read
var pText = document.createTextNode(re sult);
var pText = document.createTextNode(re
should read
var pText = document.createTextNode(re
This example in its entirety works:
You'll need to modify your "url" parameter to point to the correct file.
<!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>
You'll need to modify your "url" parameter to point to the correct file.
ASKER
Awesome! Thanks.