From: Tony De Date: 2008-04-04T17:06:04+09:00 Subject: Re: Responding to AJAX - XMLHttpRequest Tony De wrote: > Matt Todd wrote: >> One thing to note is that it is expecting a Content-Type of >> application/json as well as the content to be in actual JSON format. >> You can simply require 'json' and call wts.to_json which should be >> sufficient for your needs. >> >> You might consider looking at Rack for building your tiny web app and >> just proxy some address to it; it really cleans up handling requests >> and not needing CGI, but your solution does work fine and avoids a >> problem with AJAX requests failing due to cross-server requests (which >> is what requests not on port 80 are considered). >> >> Matt Todd > > Hey Matt, > > Thanks for the awesome quick reply. I'll dig into that. That's the > first I heard of this from any blog or forum. But there's some sense to > what you say. I've got to get ruby_json on my freebsd server. I'll let > you know how it goes. Thanks! > > tonyd Ok, got json installed. Making some changes, I still get just the one reponse. It is, however, passing additional data... Before: ..data.. Now: "...data..."\n Firebug Header info: (Maybe useful for you to help me troubleshoot) Response Headers Date Fri, 04 Apr 2008 07:46:24 GMT Server Apache/2.2.0 (FreeBSD) mod_ssl/2.2.0 OpenSSL/0.9.7e-p1 DAV/2 mod_perl/2.0.2 Perl/v5.8.8 Content-Length 159 Keep-Alive timeout=5, max=100 Connection Keep-Alive Content-Type text/html Request Headers Host www.myserver.net User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12 Accept text/javascript, text/html, application/xml, text/xml, */* Accept-Language en-us Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 300 Connection keep-alive X-Requested-With XMLHttpRequest X-Prototype-Version 1.6.0.2 Referer http://www.myserver.net/searchlog.html Authorization Basic dGVjaDp0M2No searchlog.rb --------------------- #!/usr/local/bin/ruby -w require 'rubygems' require 'json' require 'cgi' @cgi = CGI.new # Our search params passed from our HTML page sstring = @cgi['search'] slogtype = @cgi['logtype'] smethod = @cgi['method'] # A hash of possible logs logs = {'smtp' => 'smtp', 'mail' => 'mails', 'mx' => 'mx', 'spam' => 'spam', 'Dial Up' => 'radius', 'DHCP' => 'dhcp' } # Send Response def send_response(wts) @cgi.out{ wts.to_json } end ... ... # Search through the log @cgi.header { "Content-Type: application/json" } # Send only the one header back (made no diff to include with each response) File.open("/var/log/#{logs[slogtype]}", 'r').grep(/#{search_string}/).each do |line| send_response( "#{line}
" ) # send response for each result end -- Posted via http://www.ruby-forum.com/.