標識grails控制器中的ajax請求或瀏覽器請求

[英]Identifying ajax request or browser request in grails controller


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,它應該會將我帶到home/index頁面,而不是請求的頁面。下面是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/list in 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.

如何在控制器中驗證。

3 个解决方案

#1


34  

It's quite a common practice to add this dynamic method in your BootStrap.init closure:

在引導程序中添加這個動態方法是很常見的做法。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 /中完整的示例:

import javax.servlet.http.HttpServletRequest

class BootStrap {

     def init = { servletContext ->

        HttpServletRequest.metaClass.isXhr = {->
            'XMLHttpRequest' == delegate.getHeader('X-Requested-With')
        }

     }
     def destroy = {
     }
} 

#2


3  

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.

看起來您正在使用prototype,它添加了一個x - requested頭,並將其設置為“XMLHttpRequest”;發現這一點可能是你最好的選擇。

#3


3  

Since Grails 1.1 an xhr property was added to the request object that allows you to detect AJAX requests. An example of it's usage is below:

因為Grails 1.1向請求對象添加了一個xhr屬性,允許您檢測AJAX請求。它的用法示例如下:

def MyController {

  def myAction() {
    if (request.xhr) {
      // send response to AJAX request  
    } else {
      // send response to non-AJAX request
    }
  }
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/03/20/729f1e0a90000a4b5ca2aac69fa67141.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com