Currently, I am utilizing the etherscan API to retrieve logs for certain events. Although my JSON parsing method seems quite traditional, an error is being thrown stating "unexpected end of JSON".
function getEventHistory() { const topic0 = web3.utils.keccak256(config.event_string); document.getElementById("topic0").innerHTML = topic0; const qry = ("https://api.etherscan.io/api?module=logs&action=getLogs"+ "&fromBlock="+10338000+ "&toBlock="+"latest"+ "&address="+config.address+ "&topic0="+topic0+ "&apikey="+config.ether_api); console.log(qry) const request = new XMLHttpRequest() request.open("GET", qry); request.send(); request.onreadystatechange=(e)=>{ // document.getElementById("rLogsResp").innerHTML = request.responseText; parseResponse(request.responseText); } } function parseResponse(jsonString) { console.log("Converting string to jsonString: "+jsonString); const jsonObj = JSON.parse(jsonString) console.log("Full Response: "+jsonObj); var logs = [] for (var i = 0; i < jsonObj.result.length; i++) { console.log("Element Number: "+i); var event = jsonObj.result[i]; console.log("Index: "+event); const parsedEvent = new Event(event); logs.push(parsedEvent) console.log(parsedEvent); } } class Event { constructor(event) {; console.log("Parse address: "+event.address); this.address = web3.utils.toAscii(event.address); console.log("Parse topics: "+event.topics); this.topics = [web3.utils.toAscii(event.topics[0]), web3.utils.toAscii(event.topics[1]), web3.utils.toAscii(event.topics[2])]; console.log("Parse data: "+event.data); this.data = web3.utils.toAscii(event.data); console.log("Parse blockNumber: "+event.blockNumber); this.blockNumber = web3.utils.toAscii(event.blockNumber); console.log("Parse timeStamp: "+event.timeStamp); ... <p>Even though I'm relatively inexperienced with JavaScript, it seems like <code>onreadystatechange
is being triggered multiple times as the issue arises onconst jsonObj = JSON.parse(jsonString)
. Nonetheless, I do end up receiving the logs from the subsequent for loop.If anyone can offer insight into what might be wrong and how to rectify it, that would be greatly appreciated!