LWC에서 API 호출. “TypeError : Failed to fetch”

Oct 22 2020

일부 공개 API에 대한 API 호출을 시도하고 매번 동일한 오류가 발생합니다. "TypeError : Failed to fetch". 사이트가 CSP 신뢰할 수있는 사이트 및 CORS 허용 원본 목록에 추가됩니다. 코드는 LWC Playground에서 제대로 작동하며 Postman에서이 전화를 걸 수 있습니다. 아래는 내 코드입니다.

let endpoint = 'https://sv443.net/jokeapi/v2/joke/Any';

        let requestParams = {
            method: 'GET',
            headers: {
                'Content-Type' : 'application/json'//,
                //'Access-Control-Allow-Origin' : '*'
            }
        };

        fetch(endpoint, requestParams)
            .then(response => {
                console.log('in response');
            })
            .catch(error => {
                console.log('error3: ' + error);
            });

답변

2 IgorVasylevskyi Oct 22 2020 at 19:22

정답은 사이트에 대해 하나의 기본 CSP 지침 ( "img-src에 대한 사이트 허용") 만 확인한 것입니다. 10 분 안에 다른 모든 지시문을 확인했을 때 응답을 받았습니다 (CSP는 작업을 시작하는 데 시간이 걸립니다).