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

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 17898 | 回復(fù): 1
打印 上一主題 下一主題

請(qǐng)教關(guān)于ruby 實(shí)現(xiàn)簡(jiǎn)單http server的問(wèn)題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2013-08-21 18:35 |只看該作者 |倒序?yàn)g覽
用ruby寫個(gè)簡(jiǎn)單的http 服務(wù)器
server = TCPServer.new('localhost',  9000)
  loop {
  client = server.accept()
  while((x = client.gets) != "\r\n")
    puts x
  end
  
  puts "正在處理數(shù)據(jù)..."+ "\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"


如果用瀏覽器訪問(wèn) http://localhost:9000的話是可以正確顯示結(jié)果xml.
但是如果網(wǎng)頁(yè)中用jqury的ajax訪問(wèn)的話,IE/FF/Chrome都一樣,一直報(bào)錯(cuò),即始終走到error:那個(gè)分支,
用firebug看了下,貌似ajax調(diào)用只是返回了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());
                           
                        });
                    }
                });
               
        });
       
});

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 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
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP