I've been trying to make an React site, which would fetch a GET-response from API and print it out to my .html-file. I've managed to fetch the file just right, but i can't access the JSON-data server sends me.
If i use no-cors in my Fetch-request, i get an opaque response containing pretty much nothing, but if i go to Developer tools i can find my data there and read it. If i do use cors, almost same thing. I get an 403-error, but my data is in the browser memory, but my code doesn't print it out. I can find the response from Network in developer tools.
Why does the server give me an error, but still i get my data? And how can i access it, if it's in the browser?
class Clock extends React.Component {
constructor(props) {
super(props)
this.state = {data2: []}
this.apihaku = this.apihaku.bind(this)
}
componentDidMount() {
this.apihaku(),
console.log("Hei")
}
apihaku () {
fetch('https://#######/mapi/profile/',
{method: 'GET', mode:'no-cors', credentials: 'include',
headers: {Accept: 'application/json'}}
).then((response) => {
console.log(response);
response.json().then((data) =>{
console.log(data);
});
});
}
render() {
return <div>
<button>Button</button>
</div>
}}
ReactDOM.render(
<Clock />,
document.getElementById('content')
)
EDIT: Error images after trying out suggestions
https://i.sstatic.net/wp693.png
https://#######/mapi/profile/open here in front of us, because you made it so easy for us to figure out what API you are actually talking about here? "And how can i access it, if it's in the browser?" - by fixing whatever you are doing wrong in talking to this mystery API in the first place, so that it doesn't respond with a status code that signals to the browser "something went wrong, discard the response body" any more.mode:'no-cors'. When you use mode: 'no-cors' you’re explicitly specifying that you want the browser to block your frontend code from accessing any data in the response body or headers. See the answers at stackoverflow.com/questions/43317967/… and stackoverflow.com/questions/42254685/…