hx-pod cover image

hx-pod

The basics: hx-post

Jan 17, 2024
08:21

Want to use POST requests in ajax, and submit forms from anywhere on the page?

As promised in the episode, here are three ways to set up your CSRF token headers:

1. Like normal in your forms:

<input type="hidden" name="_token" value=""/>

 
(the regular html way works with htmx, of course)


2. Javascript

<script>
document.body.addEventListener('htmx:configRequest', (e) => {
    e.detail.headers['X-CSRFToken'] = '';
})
</script>

(add this to the bottom of your </body> so that EVERY request gets the right headers. I love this one!)


hx-headers

<div hx-post="/your-endpoint" hx-headers='{"X-CSRF-Token": "YourCSRFTokenHere"}'> 

<!-- Your content --> 

</div>

(add this attribute along with your hx-post)

Get the Snipd
podcast app

Unlock the knowledge in podcasts with the podcast player of the future.
App store bannerPlay store banner

AI-powered
podcast player

Listen to all your favourite podcasts with AI-powered features

Discover
highlights

Listen to the best highlights from the podcasts you love and dive into the full episode

Save any
moment

Hear something you like? Tap your headphones to save it with AI-generated key takeaways

Share
& Export

Send highlights to Twitter, WhatsApp or export them to Notion, Readwise & more

AI-powered
podcast player

Listen to all your favourite podcasts with AI-powered features

Discover
highlights

Listen to the best highlights from the podcasts you love and dive into the full episode