L
L
LMI2017-07-18 15:43:14
JavaScript
LMI, 2017-07-18 15:43:14

Local request to vk api via axios returns No 'Access-Control-Allow-Origin' header is present on the requested resource?

On React, I make a request to the VK API via axios. I'm making a request to localhost.

componentDidMount() {
    axios.get('https://api.vk.com/method/groups.getById?gid=27541941&fields=members_count')
    .then(res => {
      const lol = res.data;
      console.log(lol);
    });
  }


And an error will return in the console:

MLHttpRequest cannot load https://api.vk.com/method/groups.getById?gid=39094469&fields=members_count. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://smns.loc' is therefore not allowed access.


How to fix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LMI, 2017-07-18
@LMI

SOLVED part of the problem. The point is that axios only works with json. And cross-domain requests for vk api require type: jsonp. I tried to make a jQuery request with type: jsonp and everything works. The only question is what to use on React for JSONP....?

M
Mikhail Zakharov, 2017-07-18
@cashalot

It has to do with https. The request must be either from a domain with https. Try to remove https: from url'a.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question