How to redirect Laravel route from Javascript file in blade template

22,720

In route file

Route::get('/show-all-prescription', 'prescriptionController@show_all_prescription')->name('show-all-prescription');

Then in blade file ajax request,

window.location.href = "{{ route('show-all-prescription')}}";
Share:
22,720
Arafat Rahman
Author by

Arafat Rahman

Updated on July 09, 2022

Comments

  • Arafat Rahman
    Arafat Rahman almost 2 years

    I have a prescription form and submitted the form using AJAX.

    Now I want to automatically redirect to another route from AJAX after the form successfully submitted.

    I have tried with several options like

    window.location.href = "{ url('/show-all-prescription') }"
    

    and

    {{ route('/show-all-prescription')}}
    

    AJAX CODE

    jQuery.ajax({
    url:"{{ url('/submit_prescription') }}",
    type: 'GET',
    data: {name: name, age: age, mobile_no: mobile_no},
    success:function(msg){
    
        if(msg>0)
        {
    
            // window.location.href = "{ url('/show-all-prescription') }";
    
            {{ route('/show-all-prescription')}}
    
        }
    }
    });
    

    And got the error

    Route [/show-all-prescription] not defined

    route.php

    Route::get('/show-all-prescription', 'prescriptionController@show_all_prescription');
    

    But not getting the result. Someone Help Please?

  • Ballard
    Ballard over 4 years
    You can also use the route as suggested in this answer, in your JS file simply add: window.location.href = "/show-all-prescription" Similarly, you can pass parameters like this: window.location.href = "/show-all-prescription" + perscriptionVariable While updating the route to: Route::get('/show-all-prescription/{perscriptionVariable}', 'prescriptionController@show_all_prescription');