question

GborVarga-0131 avatar image
0 Votes"
GborVarga-0131 asked SteveDunnLondon-9857 answered

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

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

dotnet-csharpdotnet-aspnet-core-blazor
blazor-1.png (8.8 KiB)
blazor-2.png (16.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GborVarga-0131 avatar image
1 Vote"
GborVarga-0131 answered GborVarga-0131 edited

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SteveDunnLondon-9857 avatar image
1 Vote"
SteveDunnLondon-9857 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.