サーバーからテキストデータを読み込む

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Sample</title>
<script type="text/javascript"><!--
function getFile() {
xmlhttp = createXMLHttpRequest();
if (xmlhttp) {
xmlhttp.onreadystatechange = check;
xmlhttp.open("get", "sample.txt", true);
xmlhttp.send(null);
}
}
function check() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("tArea").value = xmlhttp.responseText;
}
}
// XMLHttpオブジェクト作成
function createXMLHttpRequest()
{
var XMLhttpObject = null;
try{
XMLhttpObject = new XMLHttpRequest();
}catch(e){
try{
XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
return null;
}
}
}
return XMLhttpObject;
}
// --></script>
</head>
<body>
<form>
<input type="button" value="ファイルを読み込む" onClick="getFile()"><br>
<textarea cols="40" rows="20" id="tArea"></textarea>
</body>
</html>
・サンプルを実行する
Ajax逆引きクイックリファレンス 説明などは本書を参考にしてください。