xmlhttprequest.html
1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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>