简体   繁体   中英

Prevent Rails app from responding to unintended AJAX request from jQuery

I've built out a fairly complex Rails (2.3.8) app with a lot of jQuery ajax requests. There is an occasional bug, which I have difficultly replicating, where a jQuery $.ajax({..}) request will request a page it shouldn't (like the dash page, which is never called with an ajax request)...

What ensures is absolutely madness. Incredibly strange and terrible errors happen.

For at least a stopgap solution (the app is in production), how can I set up a before filter than will detect any unsolicited xhr/ajax request (or ANY such request on the given controller actions) and kill it before it hits the controller?

In any controller:

before_filter :stop_ajax, :only => [:dashboard]

application_controller.rb:

def stop_ajax
  if request.xhr?
    render :file => "#{Rails.root}/public/404.html", :status => 404
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM