パラメータを送信して、サーバ上のsample02.cgiの結果を表示します。
JavaScriptのソースコード
function send() {
var param = "param1=" + encodeURIComponent(document.form1.param1.value) +
"¶m2=" + encodeURIComponent(document.form1.param2.value);
var url = "sample02.cgi?" + param;
var request = createXMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
//受信完了時の処理
var xml = request.responseXML;
var node1 = xml.getElementsByTagName("param1")[0].firstChild;
var text1 = node1 ? decodeURIComponent(node1.nodeValue) : '';
document.form1.value1.value = text1;
var node2 = xml.getElementsByTagName("param2")[0].firstChild;
var text2 = node2 ? decodeURIComponent(node2.nodeValue) : '';
document.form1.value2.value = text2;
}
}
request.send("");
}
sample02.cgiのソースコード
※送信されたパラメータに < > があると不正なXML形式になってしまうため、エスケープしています。
#!/usr/bin/perl
use strict;
use CGI;
my $cgi = CGI->new();
my $data = '<?xml version="1.0" encoding="UTF-8"?>' .
'<test>' .
'<param1>' . CGI::escape($cgi->param('param1')) . '</param1>' .
'<param2>' . CGI::escape($cgi->param('param2')) . '</param2>' .
'</test>';
my $len = length($data);
print "Content-Type: text/xml; charset=utf-8\n";
print "Content-Length: $len\n\n";
print $data;
このページは、「パラメータを送信する」のサンプルプログラムです。