Rails检测请求是否是AJAX

时间:2022-10-16 20:14:31

In my action I wish to only respond with processing if it was called from an AJAX request. How do I check?

在我的操作中,我只希望响应来自AJAX请求的处理。我怎么检查?

I want to do something like this:

我想做这样的事情:

def action
   @model = Model.find(params[:id])

   respond_to do |format|

      if (wasAJAXRequest()) #How do I do this?

         format.html #action.html.erb

      else

         format.html {redirect_to root_url}
   end
end

3 个解决方案

#1


235  

You can check for a header[X-Requested-With] to see if it is an AJAX request. Here is a good article on how to do it.

您可以检查一个头[X-Requested-With],看看它是否是一个AJAX请求。这是一篇关于如何做到这一点的好文章。

Here is an example:

这是一个例子:

if request.xhr?
  # respond to Ajax request
else
  # respond to normal request
end

#2


15  

If you're using :remote => true in your links or forms, you'd do:

如果您在您的链接或表单中使用:remote => true,则需要:

respond_to do |format|
  format.js { #Do some stuff }

You can also check before the respond_to block by calling request.xhr?.

您还可以通过调用request.xhr?

#3


1  

Be aware that

请注意,

request.xhr? 

returns numeric or nil values not BOOLEAN values as the docs say, in accordance with =~.

返回数值或nil值,而不是像文档说的那样,按照=~。

irb(main):004:0> /hay/ =~ 'haystack'
=> 0
irb(main):006:0> /stack/ =~ 'haystack'
=> 3
irb(main):005:0> /asfd/ =~ 'haystack'
=> nil

It's based on this:

它是基于:

# File actionpack/lib/action_dispatch/http/request.rb, line 220
def xml_http_request?
  @env['HTTP_X_REQUESTED_WITH'] =~ /XMLHttpRequest/
end

so

所以

env['HTTP_X_REQUESTED_WITH'] =~ /XMLHttpRequest/  => 0

The docs:

文档:

http://apidock.com/rails/v4.2.1/ActionDispatch/Request/xml_http_request%3F

http://apidock.com/rails/v4.2.1/ActionDispatch/Request/xml_http_request%3F

#1


235  

You can check for a header[X-Requested-With] to see if it is an AJAX request. Here is a good article on how to do it.

您可以检查一个头[X-Requested-With],看看它是否是一个AJAX请求。这是一篇关于如何做到这一点的好文章。

Here is an example:

这是一个例子:

if request.xhr?
  # respond to Ajax request
else
  # respond to normal request
end

#2


15  

If you're using :remote => true in your links or forms, you'd do:

如果您在您的链接或表单中使用:remote => true,则需要:

respond_to do |format|
  format.js { #Do some stuff }

You can also check before the respond_to block by calling request.xhr?.

您还可以通过调用request.xhr?

#3


1  

Be aware that

请注意,

request.xhr? 

returns numeric or nil values not BOOLEAN values as the docs say, in accordance with =~.

返回数值或nil值,而不是像文档说的那样,按照=~。

irb(main):004:0> /hay/ =~ 'haystack'
=> 0
irb(main):006:0> /stack/ =~ 'haystack'
=> 3
irb(main):005:0> /asfd/ =~ 'haystack'
=> nil

It's based on this:

它是基于:

# File actionpack/lib/action_dispatch/http/request.rb, line 220
def xml_http_request?
  @env['HTTP_X_REQUESTED_WITH'] =~ /XMLHttpRequest/
end

so

所以

env['HTTP_X_REQUESTED_WITH'] =~ /XMLHttpRequest/  => 0

The docs:

文档:

http://apidock.com/rails/v4.2.1/ActionDispatch/Request/xml_http_request%3F

http://apidock.com/rails/v4.2.1/ActionDispatch/Request/xml_http_request%3F