Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistent SyntaxError: JSON.parse Fails Regardless of HTML or JSON Respons #2978

Open
misterbredjenn opened this issue Oct 22, 2024 · 2 comments

Comments

@misterbredjenn
Copy link

misterbredjenn commented Oct 22, 2024

I am encountering a SyntaxError in the HTMX library when making requests that return either JSON or HTML responses. Despite the requests being successful (i.e., the server responds with a status code of 200 and the expected data), the following error appears in the console:

SyntaxError: Expected property name or '}' in JSON at position 1 (line 1 column 2)
at JSON.parse ()
at S (htmx.min.js:1:5757)
at bn (htmx.min.js:1:36513)
...

Steps to Reproduce
Set up a view in Django that handles POST requests and returns either a JSON response or an HTML response.
Send a request using HTMX (e.g., via hx-post or hx-get, i try with hx-boost to and still getting the error).
Observe the console for the SyntaxError, even though the request completes successfully.
Expected Behavior
The HTMX request should process the response without throwing a SyntaxError.

Actual Behavior
The error persists in the console regardless of whether the response is HTML or JSON, causing confusion since the request appears to work correctly.

Additional Information
HTMX Version: 2.0.3
Server Response:
For JSON: {"message": "Test success!"}
Any insights into why this error occurs despite successful requests would be greatly appreciated!

@MichaelWest22
Copy link
Contributor

From the exception line/col number it shows that it was trying to parse the json of a HX-Trigger resonse header https://htmx.org/headers/hx-trigger/ that must be returning from the server response with invalid data that is not in the correct JSON format which is expected to fail with a syntax error in this situation.

The htmx code detects if the hx-trigger header in the response starts with the character { and if it does it will try and process it as JSON. It is likely that you have the hx-trigger header set to a dynamic placeholder variable wrapped in {} that is getting sent in error.

When trying to debug problems in htmx it is best to switch to using the un-minified version of htmx.js like in https://htmx.org/docs/#via-a-cdn-e-g-unpkg-com or host the full htmx.js locally as it makes reading the error responses and understanding what function broke trivial.

@misterbredjenn
Copy link
Author

my fault, the error was due to invalid JSON syntax in the hx-headers attribute in my HTML template. HTMX expects hx-headers to be a valid JSON object, and I had mistakenly used single quotes instead of double quotes.

To fix it, I updated the hx-headers attribute with proper JSON syntax by replacing single quotes with double quotes

Thanks for the help and insights!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants