我不确定我的标题是否合理。 这个代码示例可能会使事情变得清晰。 我正在使用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
更多推荐
发布评论