使用ajax的Grails提交表单

时间:2014-04-07 12:51:10

标签: jquery grails grails-controller

我正在堆叠与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代码。谢谢你的进步。

2 个答案:

答案 0 :(得分:1)

尝试用此

替换<form>
<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>

答案 1 :(得分:0)

@ 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>