I'm trying to send a simple fetch in a project that is frontend-only. I was getting a CORS error, so I added mode: "no-cors"
to the fetch, following the advice I found on another Stack Overflow question. This resolves the CORS error, but now I'm getting an 'SyntaxError: Unexpected end of input' error. None of the data is being saved to state, so the fetch is not being completed properly. I don't have access to edit the JSON file, but I can't do anything for this exercise without being able to fetch the data.
Here's what I have in my App.js:
import React, { Component } from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
withRouter
} from "react-router-dom";
import ContactList from "./components/ContactList";
class App extends Component {
constructor() {
super();
this.state = {
contacts: []
};
}
componentDidMount() {
this.fetchContacts();
}
fetchContacts = () => {
fetch("https://s3.amazonaws.com/technical-challenge/v3/contacts.json", {
mode: "no-cors"
})
.then(resp => resp.json())
.then(contacts => {
this.setState({
contacts: contacts
});
})
.catch(alert);
};
render() {
return (
<div>
{this.state.contacts.map(contact => {
return <p>{contact.name}</p>;
})}
<ContactList contacts={this.state.contacts} />
</div>
);
}
}
export default withRouter(App);