Using Ajax to access Ruby HTTP server

I wrote a very simple HTTP server at my PC by ruby. I can access the it
from IE/FF/Chrome and display the result XML correctly, however when I
tried to access it by a Jquery AJAX call, looks like it only got the
header of the response. Anyone can help?

the code of ruby

server = TCPServer.new(‘localhost’, 9000)
loop {
client = server.accept()
while((x = client.gets) != “\r\n”)
puts x
end

$result= “123”
headers = [“HTTP/1.1 200 OK”,
“Date: Tue, 14 Dec 2010 10:48:45 GMT”,
“Server: Ruby”,
“Content-Type: text/xml;charset=gb2312”,
“Content-Length: #{$result.bytesize}\r\n\r\n”].join(“\r\n”)
client.puts headers
client.puts $result

client.close
puts “Request Handled”

and the jquery code

$(document).ready(function(){
$(“#btn”).click(function(){
$.ajax({
url:“http://localhost:9000”,
type:“GET”,
dataType:“xml”,
async:true,
timeout: 2000,
error: function(xml, status, err){
alert(‘Error loading XML
document’+xml+status+err);
},
success: function(xml){
$(xml).find(“data”).each(function(i){
alert($(this).text());

                    });
                }
            });

    });

});

ok, I got the reason. AJAX should only access the URL in the same
domain. I put the AJAX code in a local HTML, hence can’t access the
server.

updated server code as below, and put the AJAX html under “/”, the
access point it “http://localhost:9000/ajax.html

require ‘webrick’

$mydata=“test1test2”
class MyServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(request, response)
response.status = 200
response.content_type = “text/xml”
response.body = $mydata
end
end

server = WEBrick::HTTPServer.new(:Port => 9000,:DocumentRoot=>Dir::pwd)
server.mount “/data”, MyServlet
trap(“INT”){ server.shutdown }
server.start

On Wed, Aug 21, 2013 at 10:19 PM, shi wudao [email protected]
wrote:

class MyServlet < WEBrick::HTTPServlet::AbstractServlet
server.start

Does jQuery code running at http://localhost:9000 ? JavaScript , like
you
said, has a security policy to limit strict to same origin, not only
domain1. If you running jQuery code at htttp://localhost, it cannot
get
access to resources at http://localhost:9000.

If you want to make cross domain AJAX request , take a look at JSONP2,
and using it in jQuery3 (with dateType JSONP).


Silence is golden.

twitter: @AccelReality
wikipedia: AleiPhoenix
blog: weblog.areverie.org
wiki: wiki.areverie.org

Yep, you’re getting nailed by cross site scripting.

We’ve used jsonp with good luck. Render already has good support for
the callback stuff. Just do render :json => whatever, :callback =>
params[:callback] assuming that you call the callback callback. JQuery
already does and their support is easy enough to use. Keeps you from
having to roll your own timeout and error handling.

From: ruby-talk [mailto:[email protected]] On Behalf Of AR
(aka AleiPhoenix)
Sent: Thursday, August 22, 2013 1:54 AM
To: [email protected]
Subject: Re: Using Ajax to access Ruby HTTP server

On Wed, Aug 21, 2013 at 10:19 PM, shi wudao
<[email protected]mailto:[email protected]> wrote:
ok, I got the reason. AJAX should only access the URL in the same
domain. I put the AJAX code in a local HTML, hence can’t access the
server.

updated server code as below, and put the AJAX html under “/”, the
access point it “http://localhost:9000/ajax.html

require ‘webrick’

$mydata=“test1test2”
class MyServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(request, response)
response.status = 200
response.content_type = “text/xml”
response.body = $mydata
end
end

server = WEBrick::HTTPServer.new(:Port => 9000,:DocumentRoot=>Dir::pwd)
server.mount “/data”, MyServlet
trap(“INT”){ server.shutdown }
server.start

Does jQuery code running at http://localhost:9000 ? JavaScript , like
you said, has a security policy to limit strict to same origin, not
only domain1. If you running jQuery code at htttp://localhost, it
cannot get access to resources at http://localhost:9000.

If you want to make cross domain AJAX request , take a look at JSONP2,
and using it in jQuery3 (with dateType JSONP).


Silence is golden.

twitter: @AccelReality
wikipedia: AleiPhoenix
blog: weblog.areverie.orghttp://weblog.areverie.org
wiki: wiki.areverie.orghttp://wiki.areverie.org