Rails响应挂在ajax请求上

时间:2014-07-25 20:59:45

标签: jquery ruby-on-rails ajax

我的控制器中有以下内容:

def get_ip
    if request.format.json?
      @request_ip = request.remote_ip
      render json: @request_ip
    end
end

当我浏览本地主机http://www.mydomain:5000/get_ip.json时,这很好用(我不会让我输入localhost,但它是......)

但是,当我尝试通过AJAX访问时:

$.ajax({
  type: "GET",
  url: url,
  dataType: 'json'
})

它只挂在浏览器中。标题看起来像:

Request URL:http://localhost:5000/get_ip.json
Request Headers
Provisional headers are shown
Accept:application/json, text/javascript, */*; q=0.01
Cache-Control:no-cache
Pragma:no-cache
Referer:http://localhost:5000/managed-systemic-circuit/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
X-CSRF-Token:Ef1cy+yTgn5ytWO0rCi9VhOkdypPR6pdr2mxiwKq2jQ=
X-Requested-With:XMLHttpRequest

当我通过Chrome浏览时,成功的标题如下所示:

Remote Address:127.0.0.1:5000
Request URL:http://localhost:5000/get_ip.json
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Cookie:__uvt=; exists=true; UUID=1261994286; _Launch-Soon_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFVEkiJTg2NDI1OGE2ZDhmNWQxYWY4MzdlODcxNzZkNGUwZDcwBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMStXeDJlSnZSQnZCQW9pekx1a0NWWU9ERldFNzJ6dy9ZdjByTHN4OGFoR1k9BjsARg%3D%3D--ec9702f40319ff280f7179635c592cd90ad2b71a; 
Host:localhost:5000
Pragma:no-cache
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Response Headersview source
Cache-Control:max-age=0, private, must-revalidate
Connection:close
Content-Type:application/json; charset=utf-8
ETag:"f528764d624db129b32c21fbca0cb8d6"
Server:thin 1.6.2 codename Doc Brown
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Request-Id:dbf9dcf3-a11f-4152-bf4b-f9e8bded56b4
X-Runtime:0.012693
X-XSS-Protection:1; mode=block

感谢您的任何建议。

0 个答案:

没有答案