JSON
はじめてJSONでの出力をして使えるようなことまでしたので、メモ。
data_api.cgi
use JSON; my $json = encode_json([@data]); print "Content-type:application/json; charset=UTF-8\n\n"; print $json;
ajax.html
<html> <head> <script type="text/javascript"> function loadData() { if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else { if (window.ActiveXObject) { xmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); } else{ xmlHttp = null; } } xmlHttp.onreadystatechange = function () { // callback 処理 if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { var res = xmlHttp.responseText; var json = eval( '(' + res + ')' ); // なんか処理 } }; xmlHttp.open('GET', '/cgi-bin/data_api.cgi', 'True'); xmlHttp.send(null); } </head> <body> <script type="text/javascript">loadData()</script> </body> </html>
非同期通信はじめてやってみたけどなかなかおもしろい。