Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Serpent's avatar

Request to api from axios (cross domain) (CORS) error

Hello. I'm try to fetch data from bank api with AXIOS and heve an error. I suffer 3 days with this error. I already read alot about CORS but didn't find an answer. I even tryed xmlHTTTPRequest with pure javascript and it worked, but AXIOS didn't! Please help.

axios code:

methods: {

    getUsd () {

        let url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5';

        axios.get(url,{}, config).then(response => {

            this.usdPrivatBank = response.data[4].buy;

            console.log(this.usdPrivatBank);
            
        });

    }

Errors from browser console:

Failed to load resource: the server responded with a status of 405 (Not Allowed)

(index):1 Failed to load https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://incomeaccounting.dev' is therefore not allowed access. The response had HTTP status code 405.

0 likes
9 replies
ejdelmonico's avatar

@Serpent Have a look at this easy to use package https://github.com/barryvdh/laravel-cors Or, try to tweak you axios config with

headers: {
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json',
    },

or, it may be some other config params so just do a search. You can just use the request method which takes in the config to make the request and that should work as long as Apache or Nginx aren't blocking it or the api server is not configured for cors.

4 likes
Serpent's avatar

@RamjithAp, @ejdelmonico your method worked only in case i have access to the server side and write it on the server. But i have not. It's bank api site i try to connect and i didn't have access to the bank server, i try use bank api to do my application.

RamjithAp's avatar

Firstly apologies I haven't seen that third-party bank API. However, have you tried like this

 return axios(url, {
      method: 'GET',
      mode: 'no-cors',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json',
      },
      withCredentials: true,
      credentials: 'same-origin',
    }).then(response => {
    })
1 like
eugenefvdm's avatar

Subscribing to this thread because I have a similar problem. In answer to @ramjithap , I tried adding all those options to the Axios call but no luck.

2 likes
LaraBABA's avatar

Any news on this, having the exact same issue with Laravel and an app in VUE not included inside the Laravel blade's templating.

nekooee's avatar

I have this problem too and I haven't been able to solve it for a few days.

Please or to participate in this conversation.