Blazor - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Gábor Varga 21 Reputation points
2022-03-29T14:57:37.95+00:00

Hi,

I have an issue with a Blazor page.
Sometimes I receive the following error message on the browser's console view: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
This is strange, because this error message is not coming always. Just when the application thinks something, and this happen:
187996-blazor-1.png

If I click to end of the line VM50:1 on the screenshot, I have this:
187908-blazor-2.png

Any idea from anybody what can cause the problem?

The worst is that in this case none of the blazor events are working. I cannot click to buttons or blazor components, because none of them are working.

Thanks!
Gabor

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,351 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,100 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Steve Dunn (London) 16 Reputation points
    2022-08-22T16:50:01.01+00:00

    You also get this if you accidentally remove app.MapBlazorHub(); in Program.cs. Ask me how I know!

    3 people found this answer helpful.
    0 comments No comments

  2. Gábor Varga 21 Reputation points
    2022-03-29T18:57:03.597+00:00

    Hi,

    It seems, I was too quick with this help request.
    In my portal I implemented a special design template and I forgot adding the following line into the <head></head>section of MainLayout:
    <base href="~/" />

    It seems after adding that forgotten line, the error mentioned previously disappeared.

    This is a tipical application developer problem, not a framework issue.
    Now I am monitoring the portal and check if the issue appears again.

    Thanks!
    Gabor

    1 person found this answer helpful.
    0 comments No comments

  3. Michael Taylor 45,996 Reputation points
    2022-03-29T15:02:01.587+00:00

    Just a guess but Blazor is expecting JSON and you're getting HTML. The HTML is probably a server side error page that is reporting that something failed. Since your server side code isn't translating it to a problem detail or other JSON result then the client side is getting the raw HTML. It isn't checking the status code and so it tries to parse HTML as JSON and fails.

    Look at the contents of the HTML page being sent to identify the underlying server error. Then verify that you are not allowing HTML to be returned as the result of errors on the server side. You should be using problem details or something equivalent.

    I don't use Blazor myself so I cannot really provide any further instructions on how to handle errors but the docs may provide more useful suggestions.

    0 comments No comments

  4. Wisdom Jere 20 Reputation points
    2023-09-02T15:55:35.3766667+00:00

    Accidentally removed app.MapBlazorHub();

    0 comments No comments