如何在javascript中获取在Tomcat中运行的应用程序的URL?(How can I get the URL of an app running in Tomcat, in javascrip

系统教程 行业动态 更新时间:2024-06-14 17:01:31
如何在javascript中获取在Tomcat中运行的应用程序的URL?(How can I get the URL of an app running in Tomcat, in javascript?)

我不确定我的标题是否合理。 这个代码示例可能会使事情变得清晰。 我正在使用Tomcat托管一个简单的Web服务。 我也有一些HTML / JavaScript调用Web服务。 我使用XMLHttp也是如此。 这是一个例子:

var xmlhttp=new XMLHttpRequest(); xmlhttp.open('GET','http://mywebservice.com/rest/main',false); //xmlhttp.open('GET','http://localhost:8080/appName/rest/main',false); xmlhttp.send(); getNames(xmlhttp);

每次在特定的Tomcat实例(本地或云)上部署此Web服务时,都需要手动修改xmlhttp.open()调用中的URL。 请参阅注释代码。

是否可以通过编程方式获取此基本URL?

I am not sure if my heading makes sense. This code example will make things clear, perhaps. I am hosting a simple Web service using Tomcat. I also have some HTML/JavaScript making calls to the web service. I use the XMLHttp for the same. This is an example:

var xmlhttp=new XMLHttpRequest(); xmlhttp.open('GET','http://mywebservice.com/rest/main',false); //xmlhttp.open('GET','http://localhost:8080/appName/rest/main',false); xmlhttp.send(); getNames(xmlhttp);

Each time I deploy this web service on a certain Tomcat instance (local or cloud), the URL in the xmlhttp.open() call needs to be manually modified. See the commented code.

Is it possible to fetch this base URL programmatically ?

最满意答案

请参阅如何从JavaScript中的字符串中提取基本URL?

pathArray = location.href.split('/');
protocol = pathArray[0];
host = pathArray[2];
url = protocol + '//' + host; 
  
 

希望它会有所帮助

plese refer How to extract base URL from a string in JavaScript?

pathArray = location.href.split('/');
protocol = pathArray[0];
host = pathArray[2];
url = protocol + '//' + host; 
  
 

hope it will help

更多推荐

本文发布于:2023-04-20 16:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/1a51fef8d98e1f4a622ecf1b8a459cdf.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   如何在   javascript   running   Tomcat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!