2 years ago

Request wantsJson returning to the view [laravel+vue+axios]

Posted 2 years ago by mlazuardy

I am making 2 request inside 1 method for my json application with Vue+ axios. so i have a BlogController like this

   public function index()
        $blogs = Blog::with('user')->get();
        if (request()->wantsJson()) {
            return $blogs;
        return view('blog.index',compact('blogs'));

where this method can i access with url ('/blog'), but when i want to use Vue to get the blog data with axios. it always return to view, and didnt get the data from inside axios. Im already set my Header like this in bootstrap.js

window.axios.defaults.headers.common = {
    'X-CSRF-TOKEN': window.App.csrfToken,
    'X-Requested-With': 'XMLHttpRequest'

and also set the header in app.blade.php

        window.App = {!! json_encode([
            'csrfToken' => csrf_token(),
            'user' => Auth::user(),
            'signedIn' => Auth::check()
        ]) !!};

what's the problem?

Please sign in or create an account to participate in this conversation.