Vaishnav Mhetre
Vaishnav Mhetre

Reputation: 199

laravel-cors not working with axios and vue-resource

As defined in documentation of barryvdh/laravel-cors, I've tried to implement laravel-cors on a fresh installation of Laravel but that doesn't work.

It yet gives the error of No Access Origin.

I'm trying to access the post request using Axios on Quasar

Any assistance will be thanked.

Specs:

  1. Laravel @ v5.4
  2. Laravel-Cors @ v0.9.2
  3. Axios @ v0.16.2
  4. Quasar @ v0.14

Laravel-Cors documentation ref.

Works on Chrome if the extension of CORS is installed and enabled, but not the case without any extension in Firefox.

Hence need to make it work without any extension on all browsers.

Upvotes: 0

Views: 639

Answers (2)

Aleksandar Gocanin
Aleksandar Gocanin

Reputation: 69

Also when you need to fetch post request from one url to another is it's necessary to set mode: cors'

fetch('http://example.com/movies.json',{
      method: 'post',
      mode: 'cors',
      credentials: 'include',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      })
  .then((response) => {
    return response.json();
  })
  .then((myJson) => {
    console.log(myJson);
  });

Upvotes: 0

Arif
Arif

Reputation: 997

you can use barryvdh/laravel-cors to allow cors
or
just add this line
header("Access-Control-Allow-Origin: *"); in the top of controller which will receive requests

and axios data will not just a JSON
you should pass it as URL search parameter using URLSearchParams object
example:

const params = new URLSearchParams();
params.append('email', email_variable);
params.append('password', password_variable);

axios.post('url_here', params)
   .then();

Upvotes: 1

Related Questions