HX-Location 响应标头

此响应标头可用于触发客户端重定向,而无需重新加载整个页面。它不会更改页面的位置,而是像跟随 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:

注释

¥Notes

3xx 响应代码不会处理响应标头。参见 响应标头

¥Response headers are not processed on 3xx response codes. see Response Headers