Accidentally removed app.MapBlazorHub();
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:
If I click to end of the line VM50:1 on the screenshot, I have this:
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
4 answers
Sort by: Newest
-
-
Steve Dunn (London) 16 Reputation points
2022-08-22T16:50:01.01+00:00 You also get this if you accidentally remove
app.MapBlazorHub();
inProgram.cs
. Ask me how I know! -
Gábor Varga 31 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 -
Michael Taylor 48,281 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.