weixin_33726943 2012-07-18 22:15 采纳率: 0%
浏览 42

Ajax和Grails的傻瓜

I am a rookie Grails user and I am completely new to AJAX. I am not exactly grasping the concept of AJAX, and the material online is fragmented.

From my understanding, in grails if I wanted to execute a method in one of my controllers when a part of my HTML doc loads I could simply use something along the lines of

<div onload="${remoteFunction(action:"foo", update:"foo"...)}" ...>
  1. How is the response from the call to the hypothetical foo returned and how can I access it in a js function?
  2. Can I return an object from a class I created from my hypothetical foo action?
  • 写回答

1条回答 默认 最新

  • weixin_33744141 2012-07-19 06:20
    关注

    On the return of the foo action you can put simple html as text or render some objects that can be used in the view.

    Here you have all the info about the Controller "render"

    http://grails.org/doc/latest/ref/Controllers/render.html

    You can have a that will be update with that data and work there with it. Then you can access to the Html and data inside that "foo" div with javascript like you usually do.

    For example:

    Controller.groovy

    // renders text to response
    render '<div id="bar" onclick="alert($('bar').val())>some text</div>'
    

    View.gsp

    //Makes the call and updates foo
    <div onload="${remoteFunction(action:"foo", update:"foo"...)}" ...>
    <div id="foo" name="foo"></div>
    

    Output

    <div onload="theAjaxJavascriptFunctionThatGrailsWillInject" ...>
    <div id="foo" name="foo">
        <div id="bar" onclick="alert($('bar').val())">some text</div>
    </div>
    

    I you return some object from the Controller.grooy then you have to treat it like this in you View.gsp

    //Makes the call and updates foo
    <div onload="${remoteFunction(action:"foo", update:"foo"...)}" ...>
    <div id="foo" name="foo">
        ${myObject.theValueIWant}
    </div>
    

    I added a javascript alert but you can do it the way you like, there are lots of ways to do it.

    Hope it helps :)

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器