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= “123”
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());
});
}
});
});
});