duanletao9487
2017-11-25 06:49
浏览 268
已采纳

在Ajax成功之后刷新/重新加载页面 - Laravel

In my laravel project, i want to refresh my page after an ajax success but my page would refresh after the success. I tried to refresh with laravel redirect in the controller and it didn't work, i have also tried to refresh in the ajax and nothing happened? How do i do this right? How do i do this right?

Controller

if(request()->ajax()) {

            //do something             
            return ['success' => 'successfully done'];
            return redirect('admin/all')->with('status','Successfully done!');

JS

<script type="text/javascript">

        $('#master').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".sub_chk").prop('checked', true);  
         } else {  
            $(".sub_chk").prop('checked',false);  
         }  
        });

        $('.approve_all').on('click', function(e) {

            var allVals = [];  
            $(".sub_chk:checked").each(function() {  
                allVals.push($(this).attr('data-id'));
            });  

            if(allVals.length <=0)  
            {  
                alert("Please select row.");  
            } 

            else {  



                var check = confirm("Are you sure you want to delete this row?");  
                if(check == true){  

                    var join_selected_values = allVals.join(","); 

                    $.ajax({
                        url: $(this).data('url'),
                        type: 'GET',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        data: 'ids='+join_selected_values,

                        success: function (data) {
                            if (data['success']) 
                            {

                                $("#" + data['tr']).slideUp("slow");
                                alert(data['success']);
                                location="/admin/all";


                            } 
                            else if (data['error']) 
                            {
                                alert(data['error']);
                            } 
                            else 
                            {
                                //alert('Whoops Something went wrong!!');
                            }
                        },
                        error: function (data) {
                            alert(data.responseText);
                        }
                    });
                                window.location.href="/your/url" ;

                  $.each(allVals, function( index, value ) 
                  {
                      $('table tr').filter("[data-row-id='" + value + "']").remove();
                  });
                }  

            }  


        $('[data-toggle=confirmation]').confirmation({
            rootSelector: '[data-toggle=confirmation]',
            onConfirm: function (event, element) {
                element.trigger('confirm');
            }
        });

        $(document).on('confirm', function (e) {
            var ele = e.target;
            e.preventDefault();

            $.ajax({
                url: ele.href,
                type: 'GET',
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                success: function (data) {
                    if (data['success']) 
                    {

                        $("#" + data['tr']).slideUp("slow");
                        alert(data['success']);
                        location="/admin/all";

                    } 
                    else if (data['error']) {
                        alert(data['error']);
                    } 
                    else 
                    {
                        alert('Whoops Something went wrong!!');
                    }
                },
                error: function (data) {
                    alert(data.responseText);
                }
            });

            return false;
        });



    });

</script>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dscss8996 2017-11-25 07:00
    已采纳

    You need to use javascript to refresh the page. You can use location.reload()

     if ( data['success'] ) 
     {
         alert(data['success']);
         location.reload();
     } 
    
    点赞 评论
  • duanpengya7074 2017-11-25 06:56

    You can try something like this

     if (data['success']) 
     {
     $("#" + data['tr']).slideUp("slow");
     location.reload();
     } 
    
    点赞 评论
  • douya6606 2019-02-23 05:56

    For those of you still looking for a Solution, This is how you can resolve this issue.

    First of all, just return a simple message in your controller.

    class SettingsController extends Controller
    {
    
    public function __construct()
        {
    
        }
    
    public function destroy($id)
        {
    
            $user = User::findOrFail($id);
            $user->delete();
            return 'Record successfully deleted';
        }
    }
    

    Secondly, Add this code in your javascript file to refresh the page.

    setTimeout(function () { document.location.reload(true); }, 5000);

    <script type="text/javascript">
    
            function deluser(id)
             {
                event.preventDefault();
    
                $.ajax({
                 headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                 url:"{{ route('settings.destroy','')}}/"+parseInt(id),
                 method: 'delete',
                 data:{
                     id: id,
                     },
    success: function(data)
                {
                    $('#validation-message').append('<div class="alert dark alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span> </button><b>'+data+'</b></div>');
                    setTimeout(function () { document.location.reload(true); }, 5000);
    
                 },
                error: function(xhr)
                {
                    $('#validation-message').html('');
                    $.each(xhr.responseJSON.errors, function(key,value) {
                        $('#validation-message').append('<div class="alert dark alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span> </button><b>'+value+'</b></div>');
                        });
                }
                });
             }
    
          </script>
    

    Finally in your HTML.

    <!DOCTYPE html>
    <html>
    <body>
    <div class="col-xl-12 form-group" id="validation-message"></div>
    <button onclick="deluser('ID_NUMBER_GOES_HERE')">Click me</button>
    </body>
    </html>
    
    点赞 评论

相关推荐 更多相似问题