最近在做一个论坛项目使用到了Thymeleaf,在使用ajax请求的时候发现无法获取BasePath。在经过一番查阅资料后终于得知如下俩种方法,在此记录以免以后用到。
[[]]
取值,否则报错。即能够可靠的实现:<script type="text/javascript" th:inline="javascript"> </script>
<script type="text/javascript" th:inline="javascript"> /*<![CDATA[*/ var basePath = [[${#httpServletRequest.getScheme() + "://" + #httpServletRequest.getServerName() + ":" + #httpServletRequest.getServerPort() + #httpServletRequest.getContextPath()}]]; $(document).ready(function () { $.ajax({ type: ‘post‘, dataType: ‘text‘, url: basePath + ‘/index/ajaxtest‘, data: {}, cache: false, async: true, success: function (data) { var data = eval(‘(‘ + data + ‘)‘); $(‘#idUser‘).text(data.name); $(‘#idMsg‘).text(data.msg); } }); }); /*]]>*/ </script>
原文:https://www.cnblogs.com/zhangruifeng/p/12347419.html