通过 ip-api 的用户位置仅适用于本地
我对网络开发非常陌生,我怀疑这是一个简单的问题。
当我通过 VScode 运行我的网站时,我的登陆页面会显示用户区域和城市名称。当我将相同的代码上传到我的网站时,它什么也没有返回。我什至无法显示错误消息以进行调试。我试图获取 location.origin 因为这个 api 显然需要一个“非空源”。我什至不完全理解 ACAO 是如何运作的。
//Display users location in welcome message via IP
function locate() {
fetch('https://ip-api.com/json/')
.then(function(response) {
response.json().then(jsonData => {
data = jsonData;
document.getElementById("welcome").innerHTML = `Come in, ${data.city}, ${data.regionName}. Everyone is welcome.`
});
})
.catch(function(error) {
//Attempt at getting some information on the issue.
document.getElementById("welcome").innerHTML = `Come in, ${location.origin}. Everyone is welcome.`
console.log(error)
});
};
感谢所有帮助和建设性批评。谢谢。
I am very new to web development and I suspect this is a simple issue.
When I run my website via VScode my landing page displays the users region and city name onload. When I upload the same code to my website it returns nothing. I'm having trouble even displaying an error message in order to debug. I tried to get location.origin because this api apparently requires a "non null origin". I don't even fully understand how ACAO works.
//Display users location in welcome message via IP
function locate() {
fetch('https://ip-api.com/json/')
.then(function(response) {
response.json().then(jsonData => {
data = jsonData;
document.getElementById("welcome").innerHTML = `Come in, ${data.city}, ${data.regionName}. Everyone is welcome.`
});
})
.catch(function(error) {
//Attempt at getting some information on the issue.
document.getElementById("welcome").innerHTML = `Come in, ${location.origin}. Everyone is welcome.`
console.log(error)
});
};
All help, and constructive criticism, is appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是我更改了 ip 信息的 api,但网站数据缓存在浏览器中。我必须切换并重新加载页面才能看到更新后的网页。
The issue here was that I changed my api for ip information but the website data was cached in the browser. I had to shift + reload the page to see the updated web page.