0

My ajax (in vue component) like this :

<template>
    ...
    <a class="text-right" @click="detail">
        Detail
    </a>
    ...
</template>
<script>
    export default{
        ...
        methods:{
            ...
            detail() {
                this.$http.post(window.BaseUrl + '/shop/',{data: JSON.stringify(this.data)}).then(function (response) {
                    ...
                }).catch(function(error){
                    ...
                });
            }
        }
    }
</script>

If user click a link, it will call detail method

In detail method used to send data via ajax

It will routes in laravel

The route like this :

Route::group(['prefix' => 'shop','as'=>'shop.'], function () {
    Route::post('/', 'ShopController@index');
    ...
});

Then the route will call shop controller

The controller like this :

public function index(Request $request)
{
    $param = $request->only('data');
    $list = $this->shop_service->getList($param['cart_cache']);
    return view('shop.index',compact('list'));
}

If the code executed, I want it will redirect to view blade laravel (return view('shop.index',compact('list'));)

How can I do it?

moses toh
  • 12,344
  • 71
  • 243
  • 443
  • Inside your `ajax request` if the request succeeded `window.location = "/whatever";` – l.g.karolos Sep 05 '17 at 10:04
  • See this question might help you, https://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – Sagar Gautam Sep 05 '17 at 10:21
  • 2
    Why you are using AJAX if you need to redirect after success? Easier way is to create a normal controller action for this. – Troyer Sep 05 '17 at 10:25
  • @Troyer, Yes it can. But in this case, I have to keep using a link with the post method. No get method. And the solution is to use ajax. If I using form submit, it change my css view – moses toh Sep 05 '17 at 10:32

1 Answers1

1

You can redirect to route in Ajax success which will call your desired funtion in controller like this:

Ajax

success:function(){
    window.location.href(base_url+"/get/data");
});

For this to work, you should have following route and controller function

Route

Route::get('/get/data','YourController@getData');

Controller

public function getData(){      

    $list = // here write code to get desired data.

    return view('shop.index',compact('list'));
}

Hope you understand.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84