Estou confuso, quando utilizei esse código:
const cep = document.getElementById(cep)
async function buscaEndereco(cep) {
try {
var consultaCEP = await fetch(https://viacep.com.br/ws/${cep}/json/)
var consultaCEPConvertida = await consultaCEP.json()
if(consultaCEPConvertida.erro) {
throw Error("CEP NÃO EXISTE!")
}
console.log(consultaCEPConvertida)
return consultaCEPConvertida;
} catch (erro) {
console.log(erro)
}
}
cep.addEventListener(focusout, buscaEndereco(cep.value))
obtive esses erros:
TypeError: Failed to fetch
at buscaEndereco (main.js:5:33)
at main.js:19:34
main.js:5 GET https://viacep.com.br/ws//json/ net::ERR_FAILED 400 (Bad Request)
index.html:1 Access to fetch at https://viacep.com.br/ws//json/ from origin http://127.0.0.1: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 requests mode to no-cors to fetch the resource with CORS disabled.
Pórem, quando eu usei cep.addEventListener("focusout", () => buscaEndereco(cep.value))
- deu certo, me explica a diferença?