gpt4 book ai didi

HTXM HX-trigger(HTXM HX触发器)

转载 作者:bug小助手 更新时间:2023-10-28 10:09:08 25 4
gpt4 key购买 nike



I am trying to trigger a get for a table based on a response header

我正在尝试根据响应头触发表的GET


<table class="table">
<thead>
<tr>
<th>Tasks</th>
<th>Date</th>
<th>Complete</th>
<th></th>
</tr>
</thead>
<tbody id="todo-table" hx-get="http://localhost:8081/data" hx-trigger="todoAdded from:body">
</tbody>
</table>

func SubmitTodo(w http.ResponseWriter, r *http.Request) {
http.Header.Add(w.Header(), "Access-Control-Allow-Methods", "*")
http.Header.Add(w.Header(), "Access-Control-Allow-Origin", "*")
http.Header.Add(w.Header(), "Access-Control-Allow-Headers", "*")

// Stop here if its Preflighted OPTIONS request
if r.Method == "OPTIONS" {
return
}

// Get the value from the form field
value := r.FormValue("todo")
if value == "" {
return
}

todoList = append(todoList, todoTask{Task: value, Done: false, InputtedDate: time.Now()})
fmt.Println(value)

w.Header().Set("HX-Trigger", "todoAdded")

// Respond with an "ok" message and set the HX-Trigger header
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}

I can see the header is set in the response but no get event is fired. If I replace "todoAdded" with "load" the get request does fire.

我可以看到在响应中设置了标头,但没有触发任何GET事件。如果我将“todoAdded”替换为“Load”,GET请求就会触发。


更多回答
优秀答案推荐

I think what you're doing is that you are submitting a form, not through AJAX, but with a full page submit. I believe that the HX-Trigger header will then not be acted upon. Try adding hx-boost to the form in order to turn the submit into an htmx AJAX request and see if the header then works.

我认为你正在做的是,你提交一个表单,而不是通过AJAX,但与一个完整的页面提交。我相信HX-Trigger头将不会被执行。尝试将hx-boost添加到表单中,以便将提交转换为htmx AJAX请求,并查看头部是否正常工作。


The docs for the HX-Trigger response headers says:

HX-Trigger响应头的文档如下:



These response headers can be used to trigger client side actions on the target element within a response to htmx.



If you are doing a normal form submit, then you are adding the header to a response to the page, not a response to htmx.

如果您正在执行普通的表单提交,那么您将标题添加到对页面的响应,而不是对htmx的响应。


更多回答

Unfortunately this didn't make a difference

不幸的是,这并没有起到什么作用

In the browser's network tab, can you see the request header HX-Request for the submit and can you see the HX-Trigger header for the response?

在浏览器的网络选项卡中,您可以看到提交的请求头HX-Request和响应的Hx-Trigger头吗?

If you aren't returning any HTML on the submit, then the htmx docs does suggest using 204 - No Content for your status code.

如果提交时没有返回任何HTML,那么htmx文档建议您使用204-No Content作为状态代码。

yes I can see the header

是的,我能看到标题。

The respective headers for both the request and the response?

请求和响应的头部分别是什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com