NEOJPK
NEOJPK

Reputation: 823

Vue js , http 405 error Laravel

I have a problem with the http calls in my api.

The thing is that i have this http POST call:

axios.post("/api/crear/dimension/", data).then((response) => {
                        this.success();
                        console.log(response.data);
                    }).catch((response) => {
                        this.error(response)//;
                    })

Using axios because vue resource is getting deprecated.

the thing is that when i want to trigger the call this happens :

GET http://localhost/api/crear/dimension 405 (Method Not Allowed)

Notice how chrome takes this as GET call instead of a POST one. IDK why

But when i do a GET call everything works fine.

this is my route file in laravel:

Route::group(['middleware' => 'auth:api'], function () {
  Route::get('/user', function (Request $request) {
    return $request->user();
  });

  Route::post('/crear/dimension/', 'AdminController@createDimension');

});

PS : Im using the passport package in laravel. Everything is configured well because i can get a GET call with no problems. The thing is in the POST call,

I did a put , patch call too. Same result as above,

i have tried with vue resource aswell but nothings works

//EDIT

I removed the slashes from the end of the api calls and i got a 401. For more info go HERE

Upvotes: 2

Views: 4444

Answers (1)

craig_h
craig_h

Reputation: 32694

vue-resource isn't being deprecated, it's simply no longer officially supported by Vue. Laravel automatically defines an interceptor for vue-resource so the csrf_token is automatically passed, you will find the following in resources/assets/js/bootstrap.js:

Vue.http.interceptors.push((request, next) => {
    request.headers['X-CSRF-TOKEN'] = Laravel.csrfToken;

    next();
});

If you are using a different ajax library then you will need to pass this csrf_token. I don't use axios but from their docs you will probably need to include something like this in your bootstrap.js file:

axios.interceptors.request.use(function (config) {
   config.headers['X-CSRF-TOKEN'] = Laravel.csrfToken;
   return config;
});

Upvotes: 1

Related Questions