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
Does jQuery code running at http://localhost:9000 ? JavaScript , like you
said, has a security policy to limit strict to same `origin`, not only
domain[1]. If you running jQuery code at htttp://localhost, it cannot get
access to resources at http://localhost:9000.
If you want to make cross domain AJAX request , take a look at JSONP[2],
and using it in jQuery[3] (with dateType `JSONP`).
We’ve used jsonp with good luck. Render already has good support for the callback stuff. Just do render :json => whatever, :callback => params[:callback] assuming that you call the callback callback. JQuery already does and their support is easy enough to use. Keeps you from having to roll your own timeout and error handling.
On Wed, Aug 21, 2013 at 10:19 PM, shi wudao <lists@ruby-forum.com<mailto:lists@ruby-forum.com>> wrote:
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.
Does jQuery code running at http://localhost:9000 ? JavaScript , like you said, has a security policy to limit strict to same `origin`, not only domain[1]. If you running jQuery code at htttp://localhost, it cannot get access to resources at http://localhost:9000.
If you want to make cross domain AJAX request , take a look at JSONP[2], and using it in jQuery[3] (with dateType `JSONP`).