_hyperscript icon indicating copy to clipboard operation
_hyperscript copied to clipboard

<newbie> POST to the server

Open yatesco opened this issue 3 years ago • 1 comments

Hi there - I'm using htmx, and having a really good time :-). However, sometimes I just want to POST to the server without returning anything.

For example, after typing a note in a textarea I want to POST the contents to the server, just to keep it safe so when the user clicks OK all the state is serverside. I can't just POST on submit because the structure I'm updating is a graph with the textareas as some of the nodes, and I really don't fancy parsing the names of textareas to determine where in the graph they are ;-).

To be explicit, I have something like this returned from the server:

<div class="new-note">
    <form>
         <input name="action" type="hidden" value='{"action":{"UpdateProposalMessage": "<some serialised path indicating where this node is>"}}' />
        <textarea name="note" hx-post="/note" hx-trigger="mouseout changed"></textarea>
    </form>
</div>

I was hoping I could do something with hyperscript, but it doesn't seem to support a "just POST this and move on with life" ;-). Am I missing something? Don't get me wrong - it all looks fantastic, and I'm really enjoying playing with it - it just seems strange that this isn't supported, so I thought I'd check before writing some javascript.

Thanks!

yatesco avatar Aug 12 '22 19:08 yatesco

Ah, I've just seen I can do this by returning 204: https://htmx.org/docs/#requests

yatesco avatar Aug 12 '22 19:08 yatesco

You can also use the hx-swap="none" approach: https://htmx.org/docs/#swapping

But a 204 response code works fine too :)

1cg avatar Oct 04 '22 20:10 1cg