【问题标题】:Grails using ajax submit a formGrails 使用 ajax 提交表单
【发布时间】:2014-04-07 12:51:10
【问题描述】:

我正在处理与 grails 中的 ajax 相关的问题,请帮忙! 在域书中,我得到了书名和书类型,然后我生成了一个控制器和基于该域的视图。然后在bookController的更新动作中,我使用jquery弹出输入书名和书类型,然后我使用ajax技术更新那本书。我希望你得到我。 这是我的代码:

更新功能

def update(Long id, Long version) {
    def bookInstance = Book.get(id)
    if (!bookInstance) {
    flash.message = message(code: 'default.not.found.message', 
                    args: [message(code: 'book.label', default: 'Book'), id])
            redirect(action: "list")
            return
     }

    if (version != null) {
       if (bookInstance.version > version) {
      bookInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
      [message(code: 'book.label', default: 'Book')] as Object[],
       "Another user has updated this Book while you were editing")

            render(view: "edit", model: [bookInstance: bookInstance])
            return
            }
        }

      bookInstance.properties = params

     if (!bookInstance.save(flush: true)) {
          render(view: "edit", model: [bookInstance: bookInstance])
          return
     }

    flash.message = message(code: 'default.updated.message', 
    args: [message(code: 'book.label', default: 'Book'), bookInstance.id])
        redirect(action: "show", id: bookInstance.id)
    }

这是文本框

<div id="dialog" title="Edit book" style="display: none">
        <form id="ajaxForm">
            Book name: <input type="text" name="name"><br>
            Book type: <input type="text" name="type"><br><br>
            <input type="submit" value="Ok">
        </form>
</div>

这就是我遇到的麻烦。

     <script>
            function showDialog() {
                $( "#dialog" ).dialog()
            }
            function getRequest(){
                $("#ajaxForm").form({
                    type: 'POST'
                    url: '/test/book/update?name=&type='
                })
            }
     </script>

请帮我完成 ajax 代码。感谢您的提前。

【问题讨论】:

    标签: jquery grails grails-controller


    【解决方案1】:

    尝试用这个替换你的&lt;form&gt;

    <g:formRemote name="myForm" url="[controller: 'book', action: 'update']">
        Book name: <input type="text" name="name"><br>
        Book type: <input type="text" name="type"><br><br>
        <input type="submit" value="Ok">
    </g:formRemote>
    

    【讨论】:

    • 谢谢,但是ajax怎么样,要求是使用jQuery ajax更新书。
    • @user3504966 如果您查看&lt;g:formatRemote&gt; 生成的代码,您会发现它确实使用了jQuery 的AJAX 支持来提交表单
    【解决方案2】:

    @user3504966 您的 ajax 本机代码中有语法错误。请进行相应更改,如下所示:

      <script>
            function showDialog() {
                $( "#dialog" ).dialog()
            }
            function getRequest(){
                $("#ajaxForm").form({
                    type: 'POST',
                    url: '/test/book/update',
                    data:{"name":nameValueHere ,"type":typeValuehere}
                });
            }
        </script>
    

    替代#2

    <script>
        $.ajax({context: $(this),
        url:"${resource()}"+"/yourcontroller/youraction",
        type:"POST",
        data:{"param0":value1,"parma1":'value2'}});
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 2012-11-16
      • 2017-02-04
      相关资源
      最近更新 更多