我在 Grails 应用程序中使用 Atmosphere 插件对客户端进行 Ajax 推送调用。基本架构是,我在服务器中有一个循环,它创建我想要推送到浏览器的数据,因此在每次迭代中它使用大气广播()方法将数据发送到客户端。
当我在循环外使用它时它工作正常,如下所示:
def builder = new JSONBuilder()
def jsonResult = builder.build{
artist = "incubus"
location = {
lat = 45.678909
lng = -14.45667
}
}
broadcaster['/atmosphere/recommend'].broadcast(jsonResult)
但是,当我在循环内以编程方式使用它时,浏览器会抛出错误:指定了无效或非法字符串”代码:“12,并且无法正常工作。
循环的简化示例如下:
[[lat:45.678909,lng:-14.45667],[lat:32.56433,lng:22.4566]].each{
def builder = new JSONBuilder()
def jsonResult = builder.build{
artist = "incubus"
location = {
lat = '"${it.lat}"'
lng = '"${it.lng}"'
}
}
broadcaster['/atmosphere/recommend'].broadcast(jsonResult)
}
任何想法为什么会发生这种情况?谢谢!