こんにちはCGI!
XMLHttpRequestでCGIにアクセスしましょう。テキストファイルにアクセスした場合とほぼ同じですね。
http://konbu.s13.xrea.com/lib/ajax/hellocgi.html
hello.cgi
#!/usr/bin/perl -w print "Content-Type: text/plain\n\n"; print "Hello World!";
Content-Typeくらいは書いてあげないと駄目らしいです。まあ別に、text/htmlにしたところで挙動は変わらないと思いますけど。
hellocgi.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml1
1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>
Ajaxの世界にこんにちは。 Hello World!
</title>
<script type="text/javascript"><!--
function loadData(){
http = new XMLHttpRequest();
http.onreadystatechange = display;
if(http){
http.open("GET", "hello.cgi", true);
http.send("");
}
}
function display(){
if(http.readyState == 4){
document.getElementById("screen").innerHTML = http.responseText;
} else {
document.getElementById("screen").innerHTML = "Wait...";
}
}
// -->
</script>
</head>
<body>
<input type="submit" name="load" value="読み込み" onClick="loadData()">
<div id="screen"></div>
</body>
</html>もうちょっと動的なCGIのレスポンスを受け取らんとおもしろくないですね。