Home > Mobile >  Laravel redirection through ajax request from blade to controller
Laravel redirection through ajax request from blade to controller

Time:11-12

I have a blade file which allows a user to delete certain items by sending an ajax request to the controller.

If all validation on the controller end passes and the item is successfully deleted, I would like for the user to be redirected back to the same page with the page being refreshed however the page isn't being refreshed and nothing seems to be happening

Blade file

    $.ajax({
        type: "post",
        url: "{{env('APP_URL')}}/ticket-dashboard/updateTicket",
        dataType:'json',
        data: {"option":option, "status":status,"ticket_id":manual_ticket_id,'completed_id':'{{$user}}',"latest_ticket_log_id":latest_ticket_log_id,_token: '{{csrf_token()}}'},
        success: function (data) {
            console.log('-------');
            console.log(data);
            if(data['updated']){
                alert("The selected task was updated and page has to be refreshed before attempting to apply action to ticket again");
            }
            else {
                //Page should have reloaded on controller side
            }
        }
    })

Controller file

if($validationPasses){
    return redirect()->route('ticket_dashboard');
}

CodePudding user response:

if($validationPasses){
    return response->json(["status" => "redirect", "url" => "ticket_dashboard"]);
}

/////// javascript ////////////

$.ajax({
        type: "post",
        url: "{{env('APP_URL')}}/ticket-dashboard/updateTicket",
        dataType:'json',
        data: {"option":option, "status":status,"ticket_id":manual_ticket_id,'completed_id':'{{$user}}',"latest_ticket_log_id":latest_ticket_log_id,_token: '{{csrf_token()}}'},
        success: function (data) {
            console.log('-------');
            console.log(data);
            if(data['updated']){
                alert("The selected task was updated and page has to be refreshed before attempting to apply action to ticket again");
            }
            else {
                if(data.status === "redirect"){
                   window.location.href = data.url;
                }
            }
        }
    })

You need tp response to ajax then you can redirect.

  • Related