D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
html
/
idoso_ant
/
Filename :
indice.php
back
Copy
<!DOCTYPE html> <html lang="pt-BR"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Consulta API</title> </head> <body> <h1>Consultar</h1> <button id="consultar">Consultar INPC </button> <div id="resultado"></div> <script> document.getElementById('consultar').addEventListener('click', () => { const tabela = 'inpc'; const apikey = '2a11bf4b-0a8c-478c-a560-0f835eb7678f'; const params = new URLSearchParams(); params.append('tabela', tabela); params.append('apikey', apikey); const url = 'https://client-api.debit.com.br/atualiza-v1/lerTabela'; fetch(url, { method: 'POST', body: params, headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, }) .then(response => { if (!response.ok) { throw new Error('Network response was not ok: ' + response.statusText); } return response.json(); }) .then(data => { // Verifica se o dado retornado é um array if (Array.isArray(data)) { const dataAtual = new Date(); const mesAtual = dataAtual.getMonth(); // Retorna o mês de 0 a 11 console.log(mesAtual); // Obtém os últimos 12 itens do array const últimos12 = data.slice(- mesAtual); var acum = 0; últimos12.forEach((valor) => { acum = acum + valor['valor']; //console.log(valor['valor']); }); acum = acum.toFixed(2); console.log(acum); // Manipular o resultado e exibir na tela const resultadoDiv = document.getElementById('resultado'); resultadoDiv.innerHTML = `<pre>${JSON.stringify(últimos12, null, 2)}</pre>`; } else { // Caso não seja um array, exibe uma mensagem document.getElementById('resultado').innerText = 'Nenhum dado encontrado.'; } }) .catch(error => { console.error('There was a problem with the fetch operation:', error); }); }); </script> </body> </html>