/    Sign up×
Community /Pin to ProfileBookmark

Using AJAX to get data from a server

I’m new to AJAX. Trying to write a program that requests a file from a server and displays the text in the requested file. Not having any luck. Nothing happens when I click on the button. Here’s the code:

[CODE]

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<body>
<head>
<title>Intro to AJAX–create and send an XMLHttpRequest object to the server</title>

<script type=”text/javascript” language=”javascript”>
<!–The object detection code –>
function goAjax()
{
var req = null;
// Is there support for native XHR object?: IE7+, Firefox, Safari, Opera
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
//create an XMLHttpRequest object
}

else if (window.ActiveXObject)
//check for Version 6
{
req = new ActiveXObject(‘MSXML2.XMLHTTP.6.0’);
//create an ActiveX XMLHTTP component
}
if (!req)
{
req = new ActiveXObject(‘MSXML2.XMLHTTP’);
//fallback to version 3
}
//Request data to be retrieved from the server
req.onreadystatechange = function() {
if(req.readyState == 4) {
document.form1.message.value = req.responseText; }
else {
document.form1.message.write(“Retrieving message. One moment…”);
}
}

goAjax.open(“GET”, “message2.txt”, “true”);
goAjax.send(null);

}

//–>
</script>
</head>

<form name=”form1″>
Name: <input type=”text” size=”50″ name=”message”> <br />
Click the button to get the message: <input type=”button” value=”Go Get It!” onClick=”goAjax()”>
</form>

</body>
</html>
[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineMay 31.2009 — I'm new to AJAX. Trying to write a program that requests a file from a server and displays the text in the requested file. Not having any luck. Nothing happens when I click on the button. [/QUOTE]Something certainly does happen - in the error console that you're ignoring.
Copy linkTweet thisAlerts:
@rnd_meMay 31.2009 — document.form1.message.write won't work...
×

Success!

Help @MichiKen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...