javascript 如何避免在 js/ajax 脚本中硬编码应用程序上下文路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18639861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:42:50  来源:igfitidea点击:

How to avoid hard-coding the application context path in js/ajax scripts

javascriptjqueryajaxspring-mvc

提问by balteo

I use Spring MVCand Javascript/ajax. I have an issue with the way my ajax scripts refer to a server-side resource.

我使用Spring MVCJavascript/ajax我的 ajax 脚本引用服务器端资源的方式有问题。

Say I have two pagesthat need to use the same server-side resourcethrough ajax:

假设我有两个页面需要通过 ajax使用相同的服务器端资源

The url for the first page is:

第一页的网址是:

  • /myapp/advertisement/28/edit
  • /myapp/signup
  • /myapp/advertisement/28/edit
  • /myapp/signup

Say the server-side resource that my ajax script need to use is:

假设我的ajax脚本需要使用的服务器端资源是:

  • /myapp/geolocation/addressAutocomplete
  • /myapp/geolocation/addressAutocomplete

As of now, I have hard-codedthe application context path i.e. /myappin my ajax script.

到目前为止,我已经在我的 ajax 脚本中对应用程序上下文路径进行了硬编码/myapp

If and when my application context path changes I need to update is all over my scripts.

如果我的应用程序上下文路径发生变化,我需要更新的就是我的脚本。

Is there a solution to that?

有解决办法吗?

采纳答案by digitaljoel

In the HTML page that includes the script you could put an HTML basetag that points to the context. See the answer to How to get domain URL and application name?

在包含脚本的 HTML 页面中,您可以放置​​一个base指向上下文的 HTML标记。请参阅如何获取域 URL 和应用程序名称的答案

And you can read about the basetag at http://www.w3schools.com/tags/tag_base.aspwhich states The <base> tag specifies the base URL/target for all relative URLs in a document.

您可以basehttp://www.w3schools.com/tags/tag_base.asp 上阅读有关该标签的信息,其中指出The <base> tag specifies the base URL/target for all relative URLs in a document.

Before deciding whether or not to use this tag, it may be worth reading answers to Is it recommended to use the <base> html tag?

在决定是否使用此标签之前,可能值得阅读是否推荐使用 <base> html 标签?

回答by axtavt

You can use $.ajaxPrefilter()to prepend context path to all jQuery AJAX requests.

您可以使用$.ajaxPrefilter()为所有 jQuery AJAX 请求添加上下文路径。

It can be configured in <script>element of your pages, where context path value is available (e.g. ${pageContext.request.contextPath}in JSP).

它可以在<script>页面元素中配置,其中上下文路径值可用(例如${pageContext.request.contextPath}在 JSP 中)。

回答by Shessuky

I had the same problem in a JSP page using an AJAX request within an external JS file, I solved the problem by using a hidden field in JSP containing the contextPath:

我在使用外部 JS 文件中的 AJAX 请求的 JSP 页面中遇到了同样的问题,我通过在包含 contextPath 的 JSP 中使用隐藏字段解决了这个问题:

<input type="hidden" id="contextPath" value="<%=request.getContextPath()%>" readonly></input>

In JS file, get the value of the hidden field:

在JS文件中,获取隐藏字段的值:

var contextPath = $('#contextPath').val();

Finally, make the request call by concatenating the context path and the relative url like:

最后,通过连接上下文路径和相对 url 来进行请求调用,例如:

$.ajax({
        type: "GET",
        contentType : "application/json",
        dataType: 'json',
        url: contextPath + '/home/reporting',
        success: function(data) {
                    }
       });

Hope this help anyone facing the same issue

希望这可以帮助任何面临同样问题的人