Rails请求.xhr?不起作用



Applications-2(Java和Rails)

轨道-3.0.3

Ruby-1.8.7

客户端-使用Java应用中的Jquery进行Ajax调用

Firebug-显示它实际上是一个Ajax XHR请求(XHR选项卡)

服务器-Rails-请求.xhr错误

ajax响应缺少任何gem(仅来自java pp的ajax请求)

我的宝石列表如下:

abstract (1.0.0)
actionmailer (3.0.3)
actionpack (3.0.3)
activemodel (3.0.3)
activerecord (3.0.3)
activeresource (3.0.3)
activesupport (3.0.3)
arel (2.0.4)
builder (2.1.2)
bundler (1.3.4)
cgi_multipart_eof_fix (2.5.0)
columnize (0.3.6)
crack (0.1.8)
daemons (1.1.0)
delayed_job (2.1.2)
erubis (2.6.6)
fastthread (1.0.1)
gem_plugin (0.2.3)
hoptoad_notifier (2.3.12)
httparty (0.6.1)
i18n (0.4.2)
linecache (0.46)
mail (2.2.10)
mime-types (1.16)
mongrel (1.1.5)
mysql2 (0.2.6)
nokogiri (1.4.4)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack-test (0.5.6)
rails (3.0.3)
railties (3.0.3)
rake (0.8.7)
rbx-require-relative (0.0.9)
ruby-debug (0.10.4)
ruby-debug-base (0.10.4)
ruby-xslt (0.9.7)
rubygems-bundler (1.1.1)
rubyzip (0.9.4)
rvm (1.11.3.6)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.23)
xml-simple (1.0.12)

我的控制器代码是

def index
    if request.xhr?
      logger.debug "  Ajax request yes"
    else
      logger.debug "  Ajax request false"
    end
    respond_to do |format|
      format.html
      format.js
    end
end

Jquery代码为:

$.ajax({
            type: "GET",            
            url:"someurl"
            cache:false,
            success: function(data) {
                alert('success');                           
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert(jqXHR.responseText);
            }

服务器日志是

Started GET "/mozart_content?id=phyp10084_sa102&dt=20130704132018&api=a3aee3fa-567e-11df-be64-7779fa786bb0&sign=WBCbzBY0GDAP6mQVLxcFZX-ES10%3D" for 192.168.42.27 at Thu Jul 04 18:49:44 +0530 2013
  Processing by MozartContentController#index as HTML
  Parameters: {"sign"=>"WBCbzBY0GDAP6mQVLxcFZX-ES10=", "api"=>"a3aee3fa-567e-11df-be64-7779fa786bb0", "dt"=>"20130704132018", "id"=>"phyp10084_sa102"}
  Ajax request false
Rendered mozart_content/index.html.erb within layouts/application (149.3ms)
Completed 200 OK in 155ms (Views: 154.3ms | ActiveRecord: 0.0ms)

这就是问题。。。Ajax不适用于跨域。

来自Firefox文档、

使用XMLHttpRequest对象发出的HTTP请求受同源政策。特别是,这意味着web应用程序使用XMLHttpRequest只能向它所在的域发出HTTP请求是从其他域加载的,而不是加载到其他域。

您在进行POST时没有指定数据。如果要执行GET或向请求添加数据,请将类型更改为GET。

是的,当通过$.ajax发布时,有时会给出false。您可以检查request.format=:js

最新更新