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

JSON response from hx-post? #3206

Open
otonoton opened this issue Feb 22, 2025 · 1 comment
Open

JSON response from hx-post? #3206

otonoton opened this issue Feb 22, 2025 · 1 comment

Comments

@otonoton
Copy link

Is there a way to get a pre-parsed JSON response in the after-request handler? I assume it's simply not setting responseType="json" in the XHR.

Thanks

@Telroshan
Copy link
Collaborator

Hey,

Disclaimer: didn't test the following at all

I assume it's simply not setting responseType="json" in the XHR

Well honestly I think it might really be this simple!
See XMLHttpRequest: responseType property.

You could hook to the htmx:beforeSend event and override the value of event.detail.xhr.responseType

If you only need to do that for certain requests, you could also check if event.detail.requestConfig.headers contains a Accept header set to application/json for example.

You could then implement a custom extension to automatically do that for certain elements, or roll your own logic as to when to set the response type to JSON or not.

Hope this helps!

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