亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標題: 請教關于ruby 實現簡單http server的問題 [打印本頁]

作者: shiwudao    時間: 2013-08-21 18:35
標題: 請教關于ruby 實現簡單http server的問題
用ruby寫個簡單的http 服務器
server = TCPServer.new('localhost',  9000)
  loop {
  client = server.accept()
  while((x = client.gets) != "\r\n")
    puts x
  end
  
  puts "正在處理數據..."+ "\r\n"


  $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"


如果用瀏覽器訪問 http://localhost:9000的話是可以正確顯示結果xml.
但是如果網頁中用jqury的ajax訪問的話,IE/FF/Chrome都一樣,一直報錯,即始終走到error:那個分支,
用firebug看了下,貌似ajax調用只是返回了response的header部分,response本身為空的,有大仙知道原因嗎?

$(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());
                           
                        });
                    }
                });
               
        });
       
});

作者: shiwudao    時間: 2013-08-21 22:20
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(ort => 9000,ocumentRoot=>Dir::pwd)
server.mount "/data", MyServlet
trap("INT"{ server.shutdown }
server.start




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2