- 论坛徽章:
- 0
|
用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());
});
}
});
});
}); |
|