When using afterInterceptor in Grails controller I would do it like this
def afterInterceptor = [action: trackPageView, only: 'show']
private trackPageView = { model ->
// so something
}
def show() {
def user = User.get(params.id)
if (request.xhr)
render view:'show', model: [user: user]
else
[user: user]
}
This is working great on non-ajax requests but on Ajax request the model is always [:].
How do I get the user object in trackPageView for Ajax requests?
To support both ajax and non-ajax request, you have to do some content negotiation and return the respective response. Here's an example straight from the documentation:
import grails.converters.JSON
import grails.converters.XML
class BookController {
def list() {
def books = Book.list()
withFormat {
html bookList: books
json { render books as JSON }
xml { render books as XML }
}
}
}
UPDATE : I'll leave the original answer in place as reference and context. Here's an answer/suggestions to your intended question.
Assuming your non-ajax call is working, and you're able to acquire the user object from the model that's passed into your trackPageView
closure, can you confirm the following:
If yes to both, then technically you should have a model with a user object inside trackPageView
since 1) you're calling the same method and 2) passing in the same params from both ajax/non-ajax calls.
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.