Loading

Tuesday, February 12, 2008

Fetching data with Ajax 2 - Sample AJAX Codes

// Fetching data with Ajax - Sample AJAX Codes
// http://localhost/ajax/index2.html

<html>
<head>
<title>Ajax at work</title>

<script language = "javascript">
var XMLHttpRequestObject = false;

var XMLHttpRequestObject = false;

try {
XMLHttpRequestObject = new ActiveXObject("MSXML2.XMLHTTP");
} catch (exception1) {
try {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
} catch (exception2) {
XMLHttpRequestObject = false;
}
}

if (!XMLHttpRequestObject && window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
}

function getData(dataSource, divID)
{
if(XMLHttpRequestObject) {
var obj = document.getElementById(divID);
XMLHttpRequestObject.open("GET", dataSource);

XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}

XMLHttpRequestObject.send(null);
}
}
</script>
</head>

<body>

<H1>Fetching data with Ajax</H1>

<form>
<input type = "button" value = "Display Message"
onclick = "getData('data.txt', 'targetDiv')">
</form>

<div id="targetDiv">
<p>The fetched data will go here.</p>
</div>

</body>
</html>

// data.txt

This text was fetched using Ajax.

SHARE TWEET

Thank you for reading this article Fetching data with Ajax 2 - Sample AJAX Codes With URL https://x-tutorials.blogspot.com/2008/02/fetching-data-with-ajax-2-sample-ajax.html. Also a time to read the other articles.

0 comments:

Write your comment for this article Fetching data with Ajax 2 - Sample AJAX Codes above!