osyris zosar

osyris zosar

  • NA
  • 289
  • 27.6k

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:

{[Route("api")]
[ApiController]
public class HomeController : ControllerBase
{
private readonly DbProductContext _context;
public HomeController(DbProductContext context)
{
_context = context;
}
[HttpGet("{id}")]
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:

console.log("test4");
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)