Calculate freight with Fetch JavaScript API

0

I'm trying to calculate freight from mail using the Fetch JavaScript API, but it's returning the following error that I do not know how to solve.

Server was unable to process request. ---> System.Xml.XmlException: Root element is missing.

const btnCalcularFrete = document.querySelector('.calcular-frete');

const url = new URL('http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx');

const params = {
  nCdServico: '04510,04014',
  sCepOrigem: '86082-018',
  sCepDestino: '13206-450',
  nVlPeso: '2',
  nCdFormato: '1',
  nVlComprimento: 20,
  nVlLargura: 20,
  nVlAltura: 20,
  nVlDiametro: '0',
  sCdMaoPropria: 'n',
  nVlValorDeclarado: 200,
  sCdAvisoRecebimento: 'n',
}

url.search = new URLSearchParams(params);

function calcularFrete() {
  fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "text/xml; charset=utf-8",
      },
    })
    .then(response => response.text())
    .then(body => {
      console.log(body);
    })
}
btnCalcularFrete.addEventListener('click', calcularFrete);
<button class="calcular-frete">Calcular</button>
    
asked by anonymous 18.10.2018 / 00:37

0 answers