I have a PHP endpoint that returns a JSON-object (maybe that is not what this is?)
{"my_array":["a","b","c","d"]}
That's what you see when you view that page. It's created using
$array = [];
$array['my_array'] = ["a","b","c","d"];
echo json_encode($array);
When I use a regular XHR call from MDN
// get XHR from MDN
function getAjax(url, success) {
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('GET', url);
xhr.onreadystatechange = function() {
if (xhr.readyState>3 && xhr.status==200) success(xhr.responseText);
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.send();
return xhr;
}
I can iterate over it using this post-call function:
getAjax('get-users.php', logOutput);
function logOutput(response) {
console.log(JSON.parse(response));
// returns: {"my_array":["a","b","c","d"]}
}
When I try to do this with fetch, it doesn't work.
fetch('get-users.php')
.then((res) => {
return res.json(); // also tried res.text();
})
.then((data) => {
console.log(data);
});
I'm not sure if the way I'm creating the output from PHP is wrong, or if my Fetch is wrong.
Here is an image of the code/console.log
Edit: I wanted to point out it's not a url/target problem, for the sake of site anonymity I whited out some parts of the link.