1
votes

I have two Route in my api.php.

Route::get('resume-list/{templateID}', 'BasicController@getAllResumes');
Route::get('resume/single/{id}', 'BasicController@getResume');

The first one works fine but the second route returns the html code of index page.

Below is the axios call for the route:

data() {
    return {
        id: this.$route.params.id,
        data: {}
    };
},
methods: {
    loadData() {
        axios.get("api/resume/single/" + this.id).then(response => {
            console.log(response);
            this.data = response;
        });
    }
},

created() {
    this.loadData();
}

Function on backend

public function getResume($id)
{
    return Basic::where('id', $id)->firstOrFail();
}

What is the cause for this code?

Image 1

Image 2

Bootstrap.js

window._ = require('lodash');
window.Popper = require('popper.js').default;

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
    require('admin-lte');
} catch (e) {}


window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';


let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
2
are you returning json from getResume?user3532758
$response = Basic::findOrFail($id); return response()->json($response); simply do this and checkAkhtar Munir
Yes, the first route is also called by axios and it works perfectly.Pawan Rai
Maybe this a typo, but the url in your api call is api/single/resume/ but laravel route is resume/single/{id}: one is single/resume the other is resume/singleuser3532758
@PawanRai can you share a screenshot of this HTML response since it could be an error response? Akhthar Munir is asking for bootstrap code as well. I think a screenshot or the code could save a lot of time.user3532758

2 Answers

2
votes

try to convert manual - https://laravel.com/docs/5.1/responses#json-responses

if it doesn't work, the problem is configuring the router

2
votes

Make your setting in this way

window._ = require('lodash');
window.Popper = require('popper.js').default;

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
    require('admin-lte');
} catch (e) {}


window.axios = require('axios');
//window.axios.defaults.baseURL = process.env.APP_URL;
window.axios.defaults.baseURL = 'http://localhost:8000';

window.axios.defaults.headers.common = {
   'X-Requested-With': 'XMLHttpRequest',
   'Content-Type':'application/json',
   'Accept':'application/json'
   };


let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

Now your axios call will become

loadData() {
    axios.get(`/api/resume/single/${this.$route.params.id}`).then(response => {
        console.log(response);
    });
}

OR

"/api/resume/single/"+this.id