【问题标题】:how refresh page if render save json succes true如果渲染保存 json 成功,如何刷新页面 true
【发布时间】:2018-05-01 02:18:47
【问题描述】:

在控制器中

if (!surveyMasterAnswerDetailInstance.save(flush: true)) {
    render([success: false, messages: surveyMasterAnswerDetailInstance.errors] as JSON)
    return
}    
    render([success: true] as JSON)
}

//在视图中

$(document).on('submit', '#creationOptionsForm', function(e){
  e.preventDefault();

  var form_data = new FormData($('#creationOptionsForm')[0]);

  var formURL = $(this).attr("action");
  $.ajax(
    {
        url : formURL,
        processData: false,
        contentType: false,
        async: false,
        cache: false,
        type: "POST",
        data : form_data,
       success: function(data){
           if(data.success == true){ // if true (1)
               setTimeout(function(){// wait for 5 secs(2)
               location.reload(); // then reload the page.(3)
              }, 5000); 
           }
        }
        ,
        error: function(jqXHR, textStatus, errorThrown)
        {
            //if fails     
        }
    });

   return true
});

【问题讨论】:

    标签: java jquery grails groovy


    【解决方案1】:

    一个完整的工作示例如下:

    index.gsp

    <!DOCTYPE html>
    <html>
    <head>
        <meta name="layout" content="main">
        <script type="text/javascript">
                $(document).on('submit', function(e){
                  e.preventDefault();
    
                  $.ajax(
                    {
                        url : "${g.createLink(controller:'testStuff', action:'jsonUrl')}",
                        processData: false,
                        contentType: false,
                        async: false,
                        cache: false,
                        type: "POST",
                        data : $('#creationOptionsForm').serialize(),
                        success: function(data, status, jqxhr) {
                            if ( jqxhr.responseJSON.success === true ){
                                setTimeout(function(){
                                    location.reload();
                                }, 5000);
                            }
                        }
                        ,
                        error: function(jqXHR, textStatus, errorThrown)
                        {
                            alert( 'Failed' );
                        }
                    });
                });
        </script>
    </head>
    <body>
    <div>
        <g:form id="creationOptionsForm">
            <g:textField name="thename" value="${params.thename}" />
            <g:submitButton name="submit" value="submit" />
        </g:form>
    </div>
    </body>
    </html>
    

    TestStuffController

    import grails.converters.JSON
    
    class TestStuffController {
    
        def index() {
            println 'index'
    
        }
    
        def jsonUrl() {
            if (!true) {
                render([success: false, messages: 'bad stuff'] as JSON)
                return
            }
            render([success: true] as JSON)
        }
    }
    

    显然,上述情况很荒谬,但所有的部分都是为了提供一个例子。

    【讨论】:

    • 我使用你的代码,但仍然 {"success":true} 页面没有重新加载
    • 那么你收到错误了吗?尝试将console.log( errorThrown ) 添加到您的错误块并检查日志
    猜你喜欢
    • 2017-08-20
    • 1970-01-01
    • 2018-01-24
    • 2019-09-19
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    相关资源
    最近更新 更多