Skip to content

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

Closed
@misterbredjenn

Description

@misterbredjenn

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions