I am currently utilizing javascript to call a Web API. Within my HTML, I have a label element with the id of "blbtest" on an aspx page.
<asp:Label ID="blbtest" Text="" runat="server ClientIDMode="Static">
My goal is to store the value of the name variable in this label, but unfortunately, I encountered an error.
Here is the JavaScript code used:
var request = new XMLHttpRequest;
request.open('GET', "https://hplussport.com/api/products?qty=2");
request.onload = function () {
var response = request.response;
var parsedData = JSON.parse(response);
console.log(parsedData);
var name = parsedData[0].name;
var products = document.createElement('li');
//products.innerHTML = name;
//document.body.appendChild(products);
var lbl = document.getElementById('<%=blbtest.ClientID%>');
lbl.innerText = name;
};
request.send();
The encountered error states: Uncaught TypeError: Cannot set properties of null (setting 'innerText') at XMLHttpRequest.request.onload (script.js:14:19)
Even though the commented lines are functioning correctly, I aim to showcase the product's name within the label element upon page load.