osyris zosar

osyris zosar

  • 1.8k
  • 273
  • 8.3k

SyntaxError: Unexpected token < in JSON at position 0

Jun 24 2021 7:26 PM

I have created a Api with React (Hooks)
and i geet getting this error:

my Api:

public class HomeController : ControllerBase
private readonly DbProductContext _context;
public HomeController(DbProductContext context)
_context = context;
public async <Task> GetSingleData(int id)
var find = await _context.products.FindAsync(id);
if (find == null)return NotFound();
return find;

and my React/Javascripts fetch part:

fetch('api/2').then(res => res.json()).then(data => 
console.log(data)).catch(error => 
console.error('Unable to get items.', error));

I have even hard-coded the (existing) id that of the product that i want to get data from 
After the Test4 console message i get the : SyntaxError: Unexpected token < in JSON at position 0
what am i doing wrong?! please help me out.

Answers (3)