我正在尝试在rails app中检测jquery发出的ajax请求。
下面是我在控制器中的代码 要求“openssl” 要求'消化/ sha2' 要求'base64'
class IndexController < ApplicationController
def index
if params['_escaped_fragment_'] == '/Home' or params['_escaped_fragment_'] == '/home'
redirect_to :controller=>'crawler', :action => 'crawlhome'
return false
elsif params['_escaped_fragment_'] == '/Tv-Listings'
redirect_to :controller=>'crawler', :action => 'crawltvlistings'
return false
elsif params['_escaped_fragment_'] == '/Movies'
redirect_to :controller=>'crawler', :action => 'crawlmovies'
return false
elsif params['_escaped_fragment_'] == '/Videos' or params['_escaped_fragment_'] == '/videos'
redirect_to :controller=>'crawler', :action => 'crawlvideos'
return false
elsif params['_escaped_fragment_'] == '/Channels' or params['_escaped_fragment_'] == '/channels'
redirect_to :controller=>'crawler', :action => 'crawlchannels'
return false
elsif params['_escaped_fragment_'] == '/Mobile-Apps' or params['_escaped_fragment_'] == '/apps'
redirect_to :controller=>'crawler', :action => 'crawlmobileapps'
return false
elsif params['_escaped_fragment_'] =~ /\/actor(.*)/
redirect_to :controller=>'crawler', :action => 'crawlactor' , :actorname => params['_escaped_fragment_'].split("/").last
return false
elsif params['_escaped_fragment_'] =~ /\/program(.*)/
redirect_to :controller=>'crawler', :action => 'crawlprogram' , :programename => params['_escaped_fragment_'].split("/").last
return false
elsif params['_escaped_fragment_'] =~ /\/channel(.*)/
redirect_to :controller=>'crawler', :action => 'crawlchannel', :channelname => params['_escaped_fragment_'].split("/").last
return false
end
if !request.xhr? // want to execute this code only when request is not ajax
@alg = "AES-256-CBC"
aes = OpenSSL::Cipher::Cipher.new(@alg)
aes.encrypt
key = aes.random_key
@pki = Base64.encode64(key).gsub(/\n/, '')
session[:key] = key
end
end
end
但在我的情况下是request.xhr?总是返回nil或0。 请帮忙