mercredi 17 avril 2019

Laravel Axios returns HTML

I am having a problem with making Axios calls from my front-end to my back-end. But the problem is I get a HTML page response instead of JSON. I guess I am missing something but I can't find what. In my Timeline component I make an Axios GET request to this web route in routes/web.php:

axios
                .get('/memory')
                .then(response => {
                    return response;
                })
                .then(json => {
                    if (json.data.success) {
                        this.memories = json.data.data;
                    } else {
                    //    TODO error handling
                    }
                })
}

This is my route:

Route::get('memory', 'MemoryController@index');

And it should return this:

   public function index()
    {

        $response = [
            'success' => true,
            'data' => [
                        [
                            'id' => 1, 'title' => 'Picknick', 'location' => 'Eindhoven',
                            'date' => 'February 20, 2019',
                            'src' => 'https://cdn.vuetifyjs.com/images/cards/house.jpg',
                        ],
                        [
                            'id' => 2, 'title' => 'Vakantie', 'location' => 'Thailand',
                            'date' => 'November 26, 2018',
                            'src' => 'https://cdn.vuetifyjs.com/images/cards/road.jpg',
                        ],
                        [
                            'id' => 3, 'title' => 'Vakantie', 'location' => 'Australie',
                            'date' => 'Maart 13, 2018',
                            'src' => 'https://cdn.vuetifyjs.com/images/cards/plane.jpg',
                        ]
                ]
            ];

        return response()->json($response, 200);
    }

However when I make the call I only get my HTML page as return instead of JSON data. The CSRF token is added in resources/js/bootstrap.js:

window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire