I'm very new to react, and new ish to nodeJS. I'm trying to test how to pull json data from my nodeJS webservices and render it in react. I'm only at the test stage but am struggling to understand what this issues is. I can get content from demo jobs json resource with:
let url = "http://codepen.io/jobs.json";
let iterator = fetch(url);
iterator
.then(response => response.json())
.then(post => alert(post.jobs[3].company_name));
}
but my own JSON resource is at http://localhost:8888
- so I've learned I need to set no-cors in the header to allow cross site resources, so interpretation for my own resource, I have tried:
let url = "http://codepen.io/jobs.json";
let iterator = fetch(url, {mode: 'no-cors'});
iterator
.then(response => response.json())
.then(post => alert(post.jobs[3].company_name));
}
But this gives me an error of: "Uncaught (in promise) SyntaxError: Unexpected end of input"
on the resonse.json()
line.
Any ideas? Over all I would really appreciate some wider react code to take the jobs list, add it to the component state and then render the list - along the lines of:
componentDidMount() {
let url = "http://codepen.io/jobs.json";
let iterator = fetch(url, {method: 'GET', mode: 'no-cors'});
iterator
.then(response => response.json())
.then(post => alert(post.jobs[3].company_name));
}
render() {
return(
<div>
<div>Items:</div>
<div>{this.state.items.map etc</div>
</div>
);
}