0

I'm using below code to do a sample REST callout from LWC

let request = new XMLHttpRequest();            
request.open("GET", "https://dummy.restapiexample.com/api/v1/employees", true);
request.send();
request.onload = () => {
     console.log('response..!', request.response);
}

I have added this "https://dummy.restapiexample.com" URL to CSP Trusted Sites also. But, I'm getting below error. enter image description here

1 Answer 1

2

In LWC if you are trying to do a callout use fetch like this.

let endPoint = "https://dummy.restapiexample.com/api/v1/employees";
fetch(endPoint, {
  method: "GET"
})
  .then((response) => response.json()) 
  /* response.json() gives us back a promise
  we need to process the promise in .then()*/
  .then((repos) => {
    this.repos = repos;
  });

and add your url to CSP trusted sites:

enter image description here

Check this for example.

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .