1

blocked by CORS policy

Тема: blocked by CORS policy

Hi guys,

I am a junior developer and I am trying to make a request to the wialon remote api, I run the tests from postman to validate that the url is correct, but when passing it to a Fetch request from the browser it gives me the following error.

Access to fetch at 'https://hst-api.wialon.com/wialon/ajax. … %TOKEN%22}' from origin 'http://localhost:5500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.


The request I am making is very simple.

I would like to know what I am doing wrong so I can fix it.

Request with Fetch

const btn = document.getElementById('btn');
const token =''
const url = `https://hst-api.wialon.com/wialon/ajax.html?svc=token/login&params={"token":"${token}"}`;

btn.addEventListener('click', ()=>{

    const requestOptions = {
        method: 'POST',
        Headers:{
          'Access-Control-Allow-Origin': '*',
          'body-type:': 'x-www-form-urlencoded'
        }
      };
      
      fetch(url, requestOptions)
        .then(response => response.Json())
        .then(result => console.log(result))
        .catch(error => console.log('error', error));
})

I appreciate any help

  • blocked by CORS policy
2

blocked by CORS policy

Re: blocked by CORS policy

You have to use wialonSDK (see https://sdk.wialon.com/playground) or wialonjs-api (https://github.com/wialon/wialonjs-api) library to communicate with Wialon. Direct request using fetch() not allowed

3

blocked by CORS policy

Re: blocked by CORS policy

shmi

Thank you very much for the clarification.