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= "<root><data>123</data></root>"
  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());

                        });
                    }
                });

        });

});

···

--
Posted via http://www.ruby-forum.com/.

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="<root><data>test1</data><data>test2</data></root>"
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

···

--
Posted via http://www.ruby-forum.com/.

Does jQuery code running at http://localhost:9000 ? JavaScript , like you
said, has a security policy to limit strict to same `origin`, not only
domain[1]. 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 JSONP[2],
and using it in jQuery[3] (with dateType `JSONP`).

[1]:

[2]: JSONP - Wikipedia
[3]: jQuery.ajax() | jQuery API Documentation

···

On Wed, Aug 21, 2013 at 10:19 PM, shi wudao <lists@ruby-forum.com> 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&quot;

require 'webrick'

$mydata="<root><data>test1</data><data>test2</data></root>"
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

--
Silence is golden.

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

Yep, you’re getting nailed by cross site scripting. http://en.wikipedia.org/wiki/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:ruby-talk-bounces@ruby-lang.org] On Behalf Of AR (aka AleiPhoenix)
Sent: Thursday, August 22, 2013 1:54 AM
To: ruby-talk@ruby-lang.org
Subject: Re: Using Ajax to access Ruby HTTP server

On Wed, Aug 21, 2013 at 10:19 PM, shi wudao <lists@ruby-forum.com<mailto:lists@ruby-forum.com>> 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="<root><data>test1</data><data>test2</data></root>"
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 domain[1]. 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 JSONP[2], and using it in jQuery[3] (with dateType `JSONP`).

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript
[2]: http://en.wikipedia.org/wiki/JSONP
[3]: http://api.jquery.com/jQuery.ajax/

--
Silence is golden.

twitter: @AccelReality
wikipedia: AleiPhoenix
blog: weblog.areverie.org<http://weblog.areverie.org>
wiki: wiki.areverie.org<http://wiki.areverie.org>