我有一个使用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
我发现了问题。它适用于:
$.getJSON($(location).attr('protocol')+"//"+$(location).attr('hostname')+"/?apirequested=userinfo", ...
let myurl = location.protocol+"//"+location.hostname+"/?apirequested=userinfo";
$.getJSON(myurl);
或者更简单
let myurl = location.origin + "/?apirequested=userinfo";
$.getJSON(myurl);