I'm trying to use javascript's fetch() api rather than the old method with xhr requests. I've googled and read/watched tutorials and they all seem to use the same example which gets me about half way to a working solution. I can't find any examples that go beyond just echoing the data to the console, and I have a feeling that fetch does more than that /s.
I have a php page that takes a get request and returns json data. so I can do this in the browser:
http://example.com/getinfo.php?record=2543
and it returns json like so: {"record":"2543","refNum":"220710-1","custName":"Test Case"}
Most of the examples I can find say something like this:
fetch('http://example.com/getinfo.php?record=2543')
.then((response) => response.json())
.then((data) => console.log(data));
Which is half an answer. It gets the fish on the line, but won't tell me how to reel it in.
The console log shows the json data like it should, but I can't, for the life of me, figure out how to use the data to fill out a js template literal for the page.
ex:
const customerInfo = `
Customer name is: ${info.custName}
Account number is: ${info.refNum}
`;
so I want to be able to do the fetch and then somehow be able to say something like:
fetch('http://example.com/getinfo.php?record=2543')
.then((response) => response.json())
.then((data) => console.log(data));
info = response; or info = data;
or in the template literal: ${data.custName} or ${response.custName} (which I've tried and gotten errors)
How do I give the returned data object a handle so I can actually use it in my code??