如何在Json调用函数中获取绝对网站URL?
我在页面加载时调用了以下函数。
http://www.test.com/home
http://biz.test.com/abhishek/favorites
http://www.test.com/chat/messages
http://prod.test.com/conversation
我想在javascript中获取绝对网站路径, 所以我很容易分配下面的变量 strLink 在这里我分配固定路径 strLink= "www.test.com"; 而我希望它是动态的。 我怎样才能获得网站 url 路径..
假设我的 url ->
http://prod.test.com/conversation
我想要 http://prod.test.com/
不是完整路径..
var strLink ;
strLink= "www.test.com"; // <- here I want to add dynamic path..
strLink = strLink + "Service/getnames.asmx/GetNameByCity"
function getName() {
$.ajax({
type: "POST",
url: strLink,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// code
}
});
}
I called below function on page load.
http://www.test.com/home
http://biz.test.com/abhishek/favorites
http://www.test.com/chat/messages
http://prod.test.com/conversation
I want to get absolute website path in javascript,
so I easily assign below variable strLink
here I assign fixed path strLink= "www.test.com"; instead I want it dynamic.
How can I get website url path..
suppose my url ->
http://prod.test.com/conversation
I want http://prod.test.com/
not full path..
var strLink ;
strLink= "www.test.com"; // <- here I want to add dynamic path..
strLink = strLink + "Service/getnames.asmx/GetNameByCity"
function getName() {
$.ajax({
type: "POST",
url: strLink,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// code
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要获取当前网页的 URL,您可以使用
location
对象。在这种情况下,您需要
location.protocol
和location.hostname
:To get the URL of the current webpage, you can use the
location
object.In this case, you need the
location.protocol
andlocation.hostname
:@lonesomeday 关于如何找出当前协议和域的说法是完全正确的。但在这种情况下,您根本不需要域名。只需指定根绝对路径:
起始
/
将始终指向当前域的根目录。@lonesomeday is entirely correct about how to find out the current protocol and domain. However in this case, you won't need the domain name at all. Just specify a root absolute path:
the starting
/
will always point to the current domain's root directory.使用
window.location.hostname
use
window.location.hostname