Fetch TypeError: Failed to execute 'fetch' on 'Window': illegal call
I am trying to use fetch to make calls to the backend from react without libs like Axios.
api.ts
export const sendSuggestion = ((data: any): Promise<any> => {
console.log(JSON.stringify(data));
const apiUrl = `/api/suggest/`;
return fetch(apiUrl, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
}).then(checkStatus)
.then(r => r.json())
.then(data => {
console.log(data);
});
});
const checkStatus = ((response: any) => {
if (response.ok) {
return response;
} else {
var error = new Error(response.statusText);
console.log(error);
//return Promise.reject(error);
}
})
Also I include an npm module which polyfill https://www.npmjs.com/package/unfetch and imports it into my code
import fetch from 'unfetch'
console.log(fetch) returns in console:
function fetch() { [native code] }
I can't figure out what the problem is.
+3
source to share