"syntaxerror: unexpected token < in json at position 0"

  • Last Update :
  • Techknowledgy :

SyntaxError: Unexpected token in JSON at position 0 Unexpected token in JSON at position 0 Unexpected token  in JSON at position 0     at parse (<anonymous>)

This is our Splunktool team suggestion ✌, we tried and its working fine
Unexpected token W in JSON at position 0​
It 's because a simple string (plain text) is returned as the response. The text is not a valid JSON. So when you try to do res.json(), it calls JSON.parse(data). Try and do it with the string you provided, and you will get the same error.​
use res.text() instead of res.json()

It would depend on your specific network and server configuration. But generally speaking you don't need a reverse proxy just because you have multiple sites and one public IP. On your web server you would just need a section like:

server {
   server_name moodle.example.com;⋮
}

Suggestion : 2

Once you can see the data that's causing the JSON parse error it will hopefully provide a clue as to why you're not getting the valid JSON you expect. Some of the most common issues that lead to this error are: To fix this error you need to figure out why you're getting HTML (or something else) instead of the JSON you expected. To do this you need to log the data you're trying to parse to the console. These errors indicate your JavaScript code expected to receive JSON but got something else instead (probably HTML in the form of a server-side error). To fix the issue you need to examine what you got instead of the expected JSON to determine what the problem is.

Use this approach if your code looks something like this:

fetch('https://example.com/some/path/to/json')
   .then(function(response) {
      return response.json();
   })
   .then(function(data) {
      // Do something with data
   });

In this case the error is thrown when response.json() tries to run and fails to parse the data from the server as JSON. You can add a function to handle the error and display the raw text of the response body from the server and log it to the console (see notes about commented lines below):

var responseClone; // 1
fetch('https://example.com/some/path/to/json')
   .then(function(response) {
      responseClone = response.clone(); // 2
      return response.json();
   })
   .then(function(data) {
      // Do something with data
   }, function(rejectionReason) { // 3
      console.log('Error parsing JSON from response:', rejectionReason, responseClone); // 4
      responseClone.text() // 5
         .then(function(bodyText) {
            console.log('Received the following instead of valid JSON:', bodyText); // 6
         });
   });

Use this method if the code that's throwing the error looks like this:

JSON.parse(data);

Suggestion : 3

Troubleshoot 'Uncaught SyntaxError: Unexpected token u in JSON at position 0' The ‘Uncaught SyntaxError: Unexpected token u in JSON at position 0’ error is caused when the client has been asked to execute JSON.parse() on a string beginning with u instead of the stringified object it was expecting. Usually this is a stringified version of the undefined primitive. If the unexpected token is a u and it is position 0 (ie the first character) then that means it tried to parse something that begins with a u. And what familiar keyword in JavaScript begins with a u?

1._
Uncaught SyntaxError: Unexpected token u in JSON at position 0
2._
JSON.parse(undefined)
3._
var obj = {};
obj.foo; >
undefined

JSON.parse(obj.foo); >
Uncaught SyntaxError: Unexpected token u in JSON at position 0
5._
// Create my response object
var obj = {
   message: 'Hello!'
}
obj.message >
   "Hello!"

// Send the content back, eg:
// this.sendContent(obj)

// Receive the content and automatically attempt to parse it
JSON.parse(obj) >
   Uncaught SyntaxError: Unexpected token o in JSON at position 1
// Note that the error is slightly different in content, but generally the same problem

// In reality, this is the shorthand for what should be happening
JSON.parse(JSON.stringify(obj)) >
   {
      message: "Hello!"
   }
6._
Uncaught( in promise) SyntaxError: Unexpected token < in JSON at position 0

Suggestion : 4

Im trying to get a single object from the api,it worked fine for other pages but for some reason it doesnt work and i get a "SyntaxError: Unexpected token < in JSON at position 0"i dont know what to do with it. I keep getting the error:"Unable to get items. SyntaxError: Unexpected token < in JSON at position 0" Asp.Net Core API published. Default page getting 500 error but Weatherforcast api working in Postman. I like to share the error but for some reason i keep getting a error on this website

react/javascript fetch part:
const fetcher = () => {
console.log("test");
fetch(`api/${id}`)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Unable to get items.', error));
}

asp net core api get with parameter:

         [HttpGet("{id}")]
         public async Task<ActionResult<Product>> GetSingleData(int id)
         {
             var find = await _context.products.FindAsync(id);
    
             if (find == null)
                 return NotFound();
    
    
             return find;
         }

when i put the response in a console.log like this:

 const fetcher = () => {
    console.log("test");
    fetch(`api/Details/${id}`)
       .then(response => console.log(response))
       .then(data => console.log(data))
       .catch(error => console.error('Unable to get items.', error));
 }

this is the output that i get:

 Response {
    type: "basic",
    url: "https://localhost:44303/Details/api/Details/1",
    redirected: false,
    status: 200,
    ok: true,
    …
 }
 body: (...)
 bodyUsed: false
 headers: Headers {}
 ok: true
 redirected: false
 status: 200
 statusText: ""
 type: "basic"
 url: "https://localhost:44303/Details/api/Details/1"
 __proto__: Response

I'm not sure why you are not following the recommended debugging steps. The network tab's shows the HTTP status and the Response view should show the content. all you have to do is read it.

You showed a request where the URL is...

 https: //localhost:44303/Details/api/Details/1

..which I assume is part of the problem. Shouldn't the URL be ... ???

 https: //localhost:44303/api/Details/1

Suggestion : 5

SyntaxError: Unexpected token < in JSON at position 0 SyntaxError: Unexpected end of JSON input However, the URL we have passed is of an HTML page and we will be getting an HTML string as the response. Hence, if you execute the above code, it will throw the error: SyntaxError: Unexpected token < in JSON at position 0. If you observe the JSON string, it starts as a valid JSON, however, the JSON is not complete. Hence it is telling 'unexpected end of JSON input'.

1._
JSON.parse("<html>")
2._
JSON.parse('{"html')
3._
import { useEffect } from "react"

function App() {
  useEffect(() => {
    const fetchData = async () => {
      const response = await fetch("https://jsonplaceholder.typicode.com/")
      const data = await response.json()
      console.log({ data })
    }

    fetchData()
  }, [])
  return <div className="App">home</div>
}

export default App