XMLHttpRequestを使ってCGIインタフェースを実装する

az-prologのでもサイトでcgiライブラリの実装が沢山あるので、
ここではAjaxを使ってhttpから入出力を行う簡単なサンプルを作ってみます。



//test.html
<html>
<meta http-equiv="Content-Type" content="text/html; charset=Shift-jis">
<script type="text/javascript" src="xmlhttprequest.js"></script>
<script type="text/javascript">
function f(){
 xmlHttp=new XMLHttpRequest();
 xmlHttp.onreadystatechange=function(){
  if(xmlHttp.readyState==4&&xmlHttp.status==200){
   document.getElementById("out1").innerHTML=xmlHttp.responseText;
  }
 }
 str=document.getElementById("in1").value;
 xmlHttp.open("GET", "echo.cgi?formula="+str, false);
 xmlHttp.send(null);
};
</script>
<body>
<form>
<textarea cols="50" id="in1" rows="1" type="text" placeholder="keyword" value=""></textarea>
 <input type="button" onclick="f()"/>
<div id="out1"></div>
</form>
</body></html>

//echo.cgi
//Shift_JISと<html langの間に改行がありますが、これはapacheの仕様によるものなので
//無いとエラーになります。
top_call:-
get_params(A),
html_call(["Content-Type: text/html; charset=Shift_JIS
 
<html lang='ja'><meta charset='Shift_JIS' />",call(ans(A)),"</html>"]).
 
get_params(A):-get_param(formula,A),!.
get_params('Hello').
 
formula(F):-see(F),repeat,read(X),!,seen.
 
ans(A):-write(A),!.
ans.
最終更新:2014年05月07日 16:25