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

Posted 1 year 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 response.data 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

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

what's the problem?

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

Reply to

Use Markdown with GitHub-flavored code blocks.