xmlhttprequest.html 1.19 KB
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>XMLHttpRequestサンプル</title>
<script language="javascript" type="text/javascript">
//グローバル変数の定義
var httpObj;

function httpRequest() {
    // ①URLを定義
    var target_url = 'sample.txt';
    // ②XMLHttpRequestオブジェクトを生成
    httpObj = new XMLHttpRequest();
    // ③GETメソッドでHTTPリクエストを準備する
    httpObj.open("GET", target_url, true);
    // ④応答を受信できた際の処理を事前に定義しておく
    httpObj.onreadystatechange = function() {
        if (httpObj.readyState == 4 && httpObj.status == 200) {
            document.form1.textarea1.value = httpObj.responseText;
        }
    }
    //⑤HTTPリクエストを送る
    httpObj.send('');
}
</script>
</head>
<body bgcolor="#ffffff" onload="httpRequest()">

<form action="" name="form1">
<textarea name="textarea1" rows="3" cols="40"></textarea>
</form>

</body>
</html>