ajax - Grails formRemote重定向而不是仅调用方法

标签 ajax grails redirect groovy

我是Grails的新手,但g:formRemote命令遇到了一些问题。

我想要一个g:textArea框向我的 Controller 发送一条消息并保存该消息。
之后,应通过表单REMOTE Ajax 来更新页面,以便消息出现在页面上。

但是,formRemote调用假定未将页面更新,而是将给定的url视为真实链接,并希望我重定向到此(不存在).jsp网站。
我要启动的方法在 Controller 中被调用

我尝试了许多解决类似问题的解决方案,但似乎这个问题与他们的解决方案不同

这是代码:

<div id="history">
    <g:render template="posts" collection="${ messages }" var="message" />
</div>
<div class="postMessageForm">
    <g:formRemote name="postChatMessage" url="[controller: 'meetingRoom', 
                  action: 'postMessage']" update="history">                             
    <div class="msg_box">
        <g:textArea name="message" value="" style="width: 630px"/><br/>
    </div>
    <div style="float: right;">
        <g:submitButton name="Send" style="width: 90px; height: 40px;"/>
    </div>
    </g:formRemote>
</div>

这是在我的MeetingRoomController中调用的Action:
def postMessage() {
if (params.message != "") {
    def thisUser = lookUpUser()
    def thisRoom = thisUser.joinedRoom
    def chatPost = new ChatPost(
        message: params.message,
        author: thisUser
    )
    thisRoom.addToChatHistory(chatPost)
}
//  def messages = currentChatHistory()
//  render template: 'posts', collection: messages, var: 'message'

我在Jeff Browns Twitter教程中看到了这种方法。

我看到的可能的故障:
  • 注释掉的渲染模板命令与Ajax有关(当我不对其进行评论时,唯一发生的事情是模板帖子将在重定向页面
  • 上渲染
    Ajax和jQuery的
  • 用法(但我不认为这很重要,因为我刚刚使用过g:和groovy的东西,甚至还没有导入jQuery lib)
  • 使用remoteFunction可能会更容易(在这种情况下,我真的不知道如何使remoteFunction工作)

  • 我希望这些信息足以让某人看到我的缺失

    最佳答案

    在表单上单击“提交”按钮后,数据将发送到formRemote标记的url参数中列出的方法。然后进入该方法,进入注释掉的render标签,该标签将数据输出回formRemote标签的update标签中提到的div中的gsp页面。

    formRemote依赖javascript库来处理grails文档中提到的ajax内容:

    7.7.1 Ajax Support

    By default Grails ships with the jQuery library, but through the Plugin system provides support for other frameworks such as Prototype, Dojo:http://dojotoolkit.org/, Yahoo UI:http://developer.yahoo.com/yui/ and the Google Web Toolkit. This section covers Grails' support for Ajax in general. To get started, add this line to the tag of your page:

    You can replace jQuery with any other library supplied by a plugin you have installed. This works because of Grails' support for adaptive tag libraries. Thanks to Grails' plugin system there is support for a number of different Ajax libraries including (but not limited to):

    jQuery Prototype Dojo YUI MooTools



    因此,请删除历史记录div中的内容,取消注释postMessage方法中的两行,并包括引用的JavaScript库之一。

    关于ajax - Grails formRemote重定向而不是仅调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11374876/

    相关文章:

    javascript - 使用 AJAX 停留在同一页面时出现未知错误

    javascript - localStorage不会存储数据

    apache - 在 Apache 中以单一重定向重定向到 https 和 www

    javascript - 我如何调整此代码以在 Google 协作平台上运行?

    codeigniter - 如何解决 Internet Explorer 中的重定向 url 问题

    javascript - 单击时使用 ajax 调用重新加载数据表数据

    unit-testing - 单元测试在 Grails 中使用 JsonBuilder 呈现 Json

    grails - Grails 3.3.0/Gorm:如何通过联接进行查询

    tomcat - 在 Tomcat 中获取 WebUtils.retrieveGrailsWebRequest()

    javascript - Fat-Free Framework 和 Abide AJAX 表单提交不起作用