There are a couple of problems:
- You're not checking that the HTTP request succeeded. Lots of people make this mistake, it's a flaw in the
fetch API design, more here on my anemic little blog. You need to check response.ok.
response is an object with no own, enumerable properties, so JSON.stringify will return "{}" for it. To get the response, you must read the response body via one of the response object's methods, such as text, json, arrayBuffer, formData, or blob.
So for instance:
fetch("http://mylaravelapp.com/api/list")
.then(response => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.text(); // or .json(), .arrayBuffer(), ...
})
.then(data => {
console.log(JSON.stringify(data));
})
.catch(error => {
console.error(error.message);
});
fetch, no.