微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Thymeleaf获取应用程序上下文,拼接Ajax请求路径

配置文件

# 端口号设置
server.port: 8082
# 应用程序的上下文路径设置
server.servlet.context-path: /jmw

原生Ajax请求

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <Meta charset="UTF-8">
    <title>获取应用程序上下文</title>
</head>
<body>
    <button id="btn">发送原生Ajax请求</button>
</body>
<script th:src="@{/js/jquery.min.js}"></script>
<script th:inline="javascript" type="text/javascript">

	// 通过Thymeleaf内置函数获取应用程序上下文路径
    const ctxPath = [[${#httpServletRequest.getcontextpath()}]];
    console.log(ctxPath);  // /jmw

    $("#btn").on("click", function() {
        $.get(`${ctxPath}/init/test`, function(data, status){
            console.log(status);
        });
    });
</script>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐