JSONでのやり取りはできた感じ

誰が文字コード変換をしてくれてるのかが、いまいち謎なのですが。

サーバーサイドは、Ruby 1.9.2の標準添付ライブラリで、cgijsonを利用。
JavaScriptでは、jQueryと、jquery-jsonを使ってます。

[json_test.html]の一部










<input type="button" value="JSON TEST" onclick="json_test('JSON TEST');" />
<input type="button" value="日本語のJSON TEST" onclick="json_test('日本語のJSON TEST');" />

[json_test.js]


function json_test(message) {
obj = { message: message }
req = $.ajax({
type: "POST",
url: "http://painomi.sakura.ne.jp/interface/json_test.cgi",
data: "command=echo&data="+ $.toJSON(obj),
success: function(msg){
res = $.secureEvalJSON(msg);
if(res.error){alert(res.error);}
alert(res.data);
}
});
}

[json_test.cgi]


#!/home/painomi/local/bin/ruby
# -*- coding: euc-jp -*-
require 'cgi'
require 'json'

begin
cgi = CGI.new
command = cgi.params['command'][0]
data = JSON.parse(cgi.params['data'][0])

case command
when 'echo' then
result = {'error'=> nil, 'data'=> data['message'] }
else
error_str = 'command='+ command+ ', data='+ data.inspect
result = {'error'=> 'Unknown:'+ error_str, 'data'=> nil}
end

puts "Content-type: application/json; charset=utf-8\n\n";
puts JSON.pretty_generate(result)
end