java 在 grails 控制器中识别 ajax 请求或浏览器请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/665067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identifying ajax request or browser request in grails controller
提问by DonX
I am developing a grails application which uses lot of ajax.If the request is ajax call then it should give response(this part is working), however if I type in the URL in the browser it should take me to the home/index page instead of the requested page.Below is the sample gsp code for ajax call.
我正在开发一个使用大量 ajax 的 grails 应用程序。如果请求是 ajax 调用,那么它应该给出响应(这部分正在工作),但是如果我在浏览器中输入 URL,它应该带我到主页/索引页面而不是请求的页面。下面是 ajax 调用的示例 gsp 代码。
<g:remoteFunction action="list" controller="todo" update="todo-ajax">
<div id ="todo-ajax">
//ajax call rendered in this area
</div>
if we type http://localhost:8080/Dash/todo/listin the browser URL bar, the controller should redirect to http://localhost:8080/Dash/auth/index
如果我们在浏览器 URL 栏中输入http://localhost:8080/Dash/todo/list,控制器应该重定向到http://localhost:8080/Dash/auth/index
How to validate this in controller.
如何在控制器中验证这一点。
回答by Siegfried Puchbauer
It's quite a common practice to add this dynamic method in your BootStrap.init closure:
在 BootStrap.init 闭包中添加这个动态方法是很常见的做法:
HttpServletRequest.metaClass.isXhr = {->
'XMLHttpRequest' == delegate.getHeader('X-Requested-With')
}
this allows you to test if the current request is an ajax call by doing:
这允许您通过执行以下操作来测试当前请求是否是 ajax 调用:
if(request.xhr) { ... }
The simplest solution is to add something like this to your todo action:
最简单的解决方案是在您的待办事项操作中添加如下内容:
if(!request.xhr) {
redirect(controller: 'auth', action: 'index')
return false
}
You could also use filters/interceptors. I've built a solution where I annotated all actions that are ajax-only with a custom annotation, and then validated this in a filter.
您还可以使用过滤器/拦截器。我已经构建了一个解决方案,在该解决方案中,我使用自定义注释注释了所有仅 ajax 的操作,然后在过滤器中对其进行了验证。
Full example of grails-app/conf/BootStrap.groovy:
grails-app/conf/BootStrap.groovy 的完整示例:
import javax.servlet.http.HttpServletRequest
class BootStrap {
def init = { servletContext ->
HttpServletRequest.metaClass.isXhr = {->
'XMLHttpRequest' == delegate.getHeader('X-Requested-With')
}
}
def destroy = {
}
}
回答by Dónal
Since Grails 1.1 an xhrproperty was added to the requestobject that allows you to detect AJAX requests. An example of it's usage is below:
从 Grails 1.1xhr开始,request对象中添加了一个属性,允许您检测 AJAX 请求。它的用法示例如下:
def MyController {
def myAction() {
if (request.xhr) {
// send response to AJAX request
} else {
// send response to non-AJAX request
}
}
}
回答by MarkusQ
The normal method is to have the ajax routine add a header or a query string to the request and detect that. If you're using a library for the ajax, it probably provides this already.
通常的方法是让 ajax 例程向请求添加标头或查询字符串并检测它。如果您使用 ajax 库,它可能已经提供了。
It looks like you're using prototype, which adds an X-Requested-With header set to 'XMLHttpRequest'; detecting that is probably your best bet.
看起来您正在使用原型,它添加了一个X-Requested-With 标头设置为 'XMLHttpRequest';检测这可能是你最好的选择。

