Naveed Sheriffdeen
Naveed Sheriffdeen

Reputation: 980

axios get passing parameter to laravel route

I am trying to pass an id through axios.get in vue.js to laravel route. my axios code plus parameter are as following,

axios.get('http://localhost/laravel_back/public/api/bpaper',{
  params: {
    id:12
}

and my laravel route is as follows,

Route::get('bpaper/{id}', function($id)
{
return 'Paper '.$id;
});

when executing this code i get a 404 error on my browser console. and the request url is,

Request URL:http://localhost/laravel_back/public/api/bpaper?id=12

I have already given the access-control allow methods to allow communication through axios. And the code runs when not providing a parameter. any one know a fix.

Upvotes: 1

Views: 12497

Answers (1)

acdcjunior
acdcjunior

Reputation: 135752

Considerind server-side is Route::get('bpaper/{id}', function($id) { ..., the id is part of the path, not a parameter. Add it to the URL. Do:

var myId = 12;
axios.get('http://localhost/laravel_back/public/api/bpaper/' + myId)

Added it to a myId variable for clarity, you don't have to do it. Using:

axios.get('http://localhost/laravel_back/public/api/bpaper/12')

would work just as well.

Also, if you have access to newer versions of JavaScript, you can profit from template strings:

var myId = 12;
axios.get(`http://localhost/laravel_back/public/api/bpaper/${myId}`)

Upvotes: 4

Related Questions