Amir Alibašić
Amir Alibašić

Reputation: 27

Cannot fetch JSON file in React

I think this is too basic a question, but this is the problem I have right now and I cannot find a solution.

I am trying to show data from API I created on postman.

React.js code is here:

  const urlCategories = '127.0.0.1:8000/api/categories';
  const [service, setService] = useState([]);
  const fetchCategories = async () => {
    const response = await fetch(urlCategories);
    const service = await response.json();
    console.log(service);
  };
  useEffect(() => {
    fetchCategories();
  }, []);

Error in console:

Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Upvotes: 0

Views: 52

Answers (1)

Amir Alibašić
Amir Alibašić

Reputation: 27

http missing

const urlCategories = 'https://127.0.0.1:8000/api/categories';
  const [service, setService] = useState([]);
  const fetchCategories = async () => {
    const response = await fetch(urlCategories);
    const service = await response.json();
    console.log(service);
  };
  useEffect(() => {
    fetchCategories();
  }, []);

Upvotes: 1

Related Questions