grails3 Ajax请求在呈现为JSON后中断

时间:2016-05-09 11:17:53

标签: ajax grails grails-3.1

我目前正在使用grails安全插件将我的应用程序从grails 2.4.4迁移到grails 3.1.5

我使用grails ajax request plugin进行ajax调用,但在调用ajax后出现此错误。

我正在返回POJO的ArrayList而不是从API调用中获取的POGO对象

state/stateforcountry - parameters:
countryName: Ghana
Stacktrace follows:
java.lang.reflect.InvocationTargetException: null
        at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53)
        at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62)
        at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: null
        at grails.converters.JSON.value(JSON.java:180)
        at grails.converters.JSON.convertAnother(JSON.java:144)
        at grails.converters.JSON.value(JSON.java:184)
        at grails.converters.JSON.render(JSON.java:119)
        at grails.converters.JSON.render(JSON.java:132)
        at grails.artefact.controller.support.ResponseRenderer$Trait$Helper.render(ResponseRenderer.groovy:191)
        at smartinsure.admin.StateController$_stateForCountry_closure1$_closure2.doCall(StateController.groovy:29)
        at grails.artefact.Controller$Trait$Helper.withFormat(Controller.groovy:92)
        at smartinsure.admin.StateController.stateForCountry(StateController.groovy:28)
        ... 6 common frames omitted

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我终于通过使用groovy.json包中的另一个groovy转换API来解决它

def getUsers(){
   def users = userService.getAllUsers();
   render JsonOutput.toJson(users);
}

而不是render users as JSON。 JsonOutput位于groovy.json包中或更好

withFormat {
      html { render view:"edit", model:users}
      json { render JsonOutput.toJson(users)}
      xml { render users as XML}
}