I want to reference an absolute URL in application.js, but the grails.app.context changes between environments.
How can I reference grails.app.context in application.js?
I want to reference an absolute URL in application.js, but the grails.app.context changes between environments.
How can I reference grails.app.context in application.js?
Igor 建议的答案很好,我个人更喜欢将该信息存储到元标记中(在 GSP 布局中)以避免定义 javascript 全局变量。像这样的东西:
<meta name="appContext" content="${g.resource(dir: '/')}" />
因此,您可以从应用程序中的任何位置检索值。像这样:
$('meta[name=appContext]').attr('content');
您可以将上下文放入全局 JS 变量中:
<g:javascript>
window.appContext = '${request.contextPath}';
</g:javascript>
并在普通 javascript 的任何地方使用它,例如:
$.ajax({
url: appContext + '/hello/world'
})