1

I am trying to fetch some data from the server and I am using react.js and the fetch api but a get that

SyntaxError: Unexpected token < in JSON at position 0 this is my code for fetching the data

componentDidMount() {
    // fetch all categories from the server
    fetch('http:localhost:5555/categories')
        .then(res => res.json())
        .then(categories => {
            this.setState({ loading: false });
            console.log(categories);
            this.setState({ categories });
        })
        .catch(err => {
            this.setState({ loading: false });
            console.log(err);
        });
}

any thoughts

1 Answer 1

2

I think the problem is in your fetch method and you write the url in the wrong form try to change the url from

http:localhost:5555/categories

to

http://localhost:5555/categories

any try to fetch again

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.