I have a response / redux application and am trying to execute a simple GET request for a server:
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
}).then((response) => {
console.log(response.body); // null
return dispatch({
type: "GET_CALL",
response: response
});
})
.catch(error => { console.log('request failed', error); });
The problem is that the response body is empty in the function .then(), and I'm not sure why. I checked the examples online, and it looks like my code should work, so I obviously missed something. The fact is that if I check the network tab in Chrome dev tools, a request will be made and I will get the data I'm looking for.
Can anyone shine in this world?
EDIT:
I tried to convert the answer.
with the help of .text():
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
})
.then(response => response.text())
.then((response) => {
console.log(response);
return dispatch({
type: "GET_CALL",
response: response
});
})
.catch(error => { console.log('request failed', error); });
and .json():
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
})
.then(response => response.json())
.then((response) => {
console.log(response.body);
return dispatch({
type: "GET_CALL",
response: response.body
});
})
.catch(error => { console.log('request failed', error); }); // Syntax error: unexpected end of input
Search in Chrome tools:
