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