我有一个使用 getJSON 的 JQuery 函数来带来一些 userifo。是这样的:
$.getJSON("http://server.com/?apirequested=userinfo", function(data){
...
...
});
这工作正常,但我正在尝试将其更改为使用相对 URL 的多个服务器使用相同的代码。
我正在尝试以下几件事:
$.getJSON($(location).attr('hostname')+"/?apirequested=userinfo" ...
或
$.getJSON($(location).attr('protocol')+$(location).attr('hostname')+"/?apirequested=userinfo",
或
$.getJSON(location.hostame+"/?apirequested=userinfo" ...
但是它不起作用。我做错了什么?
有什么建议吗?
预先感谢您的宝贵时间。
最佳答案
这可能对你有帮助。
http://tech-blog.maddyzone.com/javascript/get-current-url-javascript-jquery
$(location).attr('host'); www.test.com:8082
$(location).attr('hostname'); www.test.com
$(location).attr('port'); 8082
$(location).attr('protocol'); http:
$(location).attr('pathname'); index.php
$(location).attr('href'); http://www.test.com:8082/index.php#tab2
$(location).attr('hash'); #tab2
$(location).attr('search'); ?foo=123
关于javascript - 在 JQuery 中获取主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49413313/