最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

htmx - Data passed via URL instead of via form data - Stack Overflow

programmeradmin1浏览0评论

Summary

htmx 1.8.0 is sending data via form data.

htmx 2.0.4 is sending data via URL.

Example

Here's a minimal version of index.html from contact-app.

<!doctype html>
<html lang="">

    <head>
        <script src="/static/js/htmx-1.8.0.js"></script>

        {# <script src="/[email protected]/dist/htmx.min.js"></script> #}
    </head>

    <body>
        <main>
            
            <form x-data="{ selected: [] }">

                <table>
                    <thead>
                    <tr>
                        <th></th> <th>First</th> <th>Last</th> <th>Phone</th> <th>Email</th> <th></th>
                    </tr>
                    </thead>
                    <tbody>

                        {% for contact in contacts %}
                            <tr>
                                <td><input type="checkbox" name="selected_contact_ids" value="{{ contact.id }}" x-model="selected"></td>
                                <td>{{ contact.first }}</td>
                                <td>{{ contact.last }}</td>
                                <td>{{ contact.phone }}</td>
                                <td>{{ contact.email }}</td>
                                <td>
                                </td>
                            </tr>
                        {% endfor %}

                    </tbody>
                </table>

                <button hx-delete="/contacts" hx-confirm="Confirm deletion" hx-target="body">
                    Delete Selected Contacts
                </button>

            </form>
        </main>
    </body>
</html>

If I select some contacts:

and click Delete Selected Contacts, DevTools shows that the contact values are sent via form data:

If instead of htmx 1.8.0 I use 2.0.4:

    {# <script src="/static/js/htmx-1.8.0.js"></script> #}

    <script src="/[email protected]/dist/htmx.min.js"></script>

the selected values are sent via URL:

Question

Is there a way to get htmx 2.0.4 to send the selected values via form data like in version 1.8.0?

Summary

htmx 1.8.0 is sending data via form data.

htmx 2.0.4 is sending data via URL.

Example

Here's a minimal version of index.html from contact-app.

<!doctype html>
<html lang="">

    <head>
        <script src="/static/js/htmx-1.8.0.js"></script>

        {# <script src="https://unpkg.com/[email protected]/dist/htmx.min.js"></script> #}
    </head>

    <body>
        <main>
            
            <form x-data="{ selected: [] }">

                <table>
                    <thead>
                    <tr>
                        <th></th> <th>First</th> <th>Last</th> <th>Phone</th> <th>Email</th> <th></th>
                    </tr>
                    </thead>
                    <tbody>

                        {% for contact in contacts %}
                            <tr>
                                <td><input type="checkbox" name="selected_contact_ids" value="{{ contact.id }}" x-model="selected"></td>
                                <td>{{ contact.first }}</td>
                                <td>{{ contact.last }}</td>
                                <td>{{ contact.phone }}</td>
                                <td>{{ contact.email }}</td>
                                <td>
                                </td>
                            </tr>
                        {% endfor %}

                    </tbody>
                </table>

                <button hx-delete="/contacts" hx-confirm="Confirm deletion" hx-target="body">
                    Delete Selected Contacts
                </button>

            </form>
        </main>
    </body>
</html>

If I select some contacts:

and click Delete Selected Contacts, DevTools shows that the contact values are sent via form data:

If instead of htmx 1.8.0 I use 2.0.4:

    {# <script src="/static/js/htmx-1.8.0.js"></script> #}

    <script src="https://unpkg.com/[email protected]/dist/htmx.min.js"></script>

the selected values are sent via URL:

Question

Is there a way to get htmx 2.0.4 to send the selected values via form data like in version 1.8.0?

Share Improve this question asked Jan 20 at 0:11 dharmatechdharmatech 9,51710 gold badges47 silver badges104 bronze badges 2
  • 1 Just guessing here, but if you add "method=POST" to the <form> tag, does that change things? – Tim Roberts Commented Jan 20 at 0:24
  • Hey @TimRoberts. I posted an answer (Thanks to Jeremy Howard in the discord channel for FastHTML). – dharmatech Commented Jan 20 at 0:26
Add a comment  | 

1 Answer 1

Reset to default 1

The migration guide:

https://htmx.org/migration-guide-htmx-1/

mentions this.

If you want DELETE requests to use a form-encoded body rather than parameters, revert htmx.config.methodsThatUseUrlParams to ["get"] (it’s a little crazy, but DELETE, according to the spec, should use request parameters like GET.)

So in the example code, the following will revert to the original behavior:

    <script>

        htmx.config.methodsThatUseUrlParams = ["get"];

    </script>
发布评论

评论列表(0)

  1. 暂无评论