关于grails/groovy中,render(contentType: "text/javascript")的记录

今天使用到了render(contentType: "text/javascript"),对于此问题,自己存在很不解,特此,做笔记,以待进一步研究,有幸的话,希望大家帮忙给解惑!

class WriteController{

  def showContent={

    String content=topicService.showContentTopics()//调用services中的showContentTopics

    return render(contentType:"text/javascirpt",text:content)

  }

}

class TopicService {

  def showContentTopics={

    def sql=""" select ...."""  //此处内容省略,为sql语句,用于查询出所要的数据

    def result=....//这个过程是调用数据库的函数,得到查询结果的集合

    ...  //根据具体情况,追加map中的数据到字符类型的变量中

    String topics=...//将多个串变量1+串变量2+...,根据需要生成;

    return topics

  }

}

上面两步,完成从控制器Controller中返回“text/javascript”类型的值,

目前我知道的方法,有两种:

第一种:可以从js文件中调用这个action

第二种:在gsp页面中通过

$ajax({

  url: "路径",

  error: function (xhr, msg, exc) {
  },

  success: function (data) {

  }

})//待验证

posted @ 2016-04-07 19:24  readygo  阅读(811)  评论(0编辑  收藏  举报