-1

I trying call an external API, who return an JSON with data who I need complete my form.

I trying do this with this code:

     methods: {
          verificarCnpj (newValue) {        
            if (newValue !== 0 && newValue.length === 14) {
              console.log('não é vazio e tamanho tem 14')
              this.$http.get(`https://www.receitaws.com.br/v1/cnpj/${newValue}`)
                .then(response => {
                  return response.body
                })
                .catch((error) => {
                  console.log(error)
                })
            }
          },
    .........

Where newValue is the return on param do pass data to the API.

Example of result of: https://www.receitaws.com.br/v1/cnpj/07623088000147

But I have this return in my console:

Access to XMLHttpRequest at 'https://www.receitaws.com.br/v1/cnpj/07623088000147' from origin 'http://localhost:8071' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This is the capture of the console errors:

enter image description here

How I Can resolve this?

Thanks, All!

Ramos
  • 159
  • 1
  • 3
  • 16

1 Answers1

-1

as far as i understand u r trying to get JSON file and completing your form using the values, first check ur database, if its okay and still doesnt work try making a GITHUB account and upload the JSON file there and give its access from there, your CORS error will disapear coz JS restricts any uploading from localServer due to security issues.

Dehya Rao
  • 13
  • 3
  • What database? The OP doesn't mention a database. – Quentin Aug 23 '19 at 13:07
  • 1
    "upload the JSON file there and give its access from there" — How do you give cross-origin access using Github? It isn't a feature that I've heard about. – Quentin Aug 23 '19 at 13:07
  • 1
    "restricts any uploading" — There's no uploading involved here. It's *downloading* that the question is about. – Quentin Aug 23 '19 at 13:08
  • 1
    "upload the JSON file" — The JSON appears to be dynamically generated by a third-party API. Saving a snapshot of it would not be useful. – Quentin Aug 23 '19 at 13:08
  • I put a snapshot in the question. @Quentin – Ramos Aug 23 '19 at 13:23
  • It's an external service, of Central Bank from Brazil. And the "NewValue" is the param to get informations of an API. – Ramos Aug 23 '19 at 13:25
  • 1
    @Ramos — Still a duplicate question. The answer is still at the other end of the link in the big yellow box at the top of the page. – Quentin Aug 23 '19 at 13:35
  • I understund now. Thanks. @Quentin – Ramos Aug 23 '19 at 14:04