此响应标头可用于触发客户端重定向,而无需重新加载整个页面。它不会更改页面的位置,而是像跟随 hx-boost
链接 一样,创建新的历史记录条目,向标头的值发出 ajax 请求,并将路径推送到历史记录中。
¥This response header can be used to trigger a client side redirection without reloading the whole page. Instead of changing the page’s location it will act like following a hx-boost
link, creating a new history entry, issuing an ajax request to the value of the header and pushing the path into history.
示例响应如下:
¥A sample response would be:
HX-Location: /test
哪个会推动客户端进行测试,就好像用户单击了 <a href="/test" hx-boost="true">
一样
¥Which would push the client to test as if the user had clicked on <a href="/test" hx-boost="true">
如果你想重定向到页面上的特定目标,而不是默认的 document.body,你可以将更多详细信息与事件一起传递,方法是使用 JSON 作为标头的值:
¥If you want to redirect to a specific target on the page rather than the default of document.body, you can pass more details along with the event, by using JSON for the value of the header:
HX-Location: {"path":"/test2", "target":"#testdiv"}
路径是必需的,是加载响应的 URL。其余数据反映了 ajax
api 上下文,即:
¥Path is required and is url to load the response from. The rest of the data mirrors the ajax
api context, which is:
source
- 请求的源元素
¥source
- the source element of the request
event
- “triggered” 请求的事件
¥event
- an event that “triggered” the request
handler
- 处理响应 HTML 的回调
¥handler
- a callback that will handle the response HTML
target
- 将响应交换到的目标
¥target
- the target to swap the response into
swap
- 响应将如何相对于目标进行交换
¥swap
- how the response will be swapped in relative to the target
values
- 随请求提交的值
¥values
- values to submit with the request
headers
- 与请求一起提交的标头
¥headers
- headers to submit with the request
select
- 允许你从响应中选择要交换的内容
¥select
- allows you to select the content you want swapped from a response
¥Notes
3xx 响应代码不会处理响应标头。参见 响应标头
¥Response headers are not processed on 3xx response codes. see Response Headers