javascript - 地理位置 API 问题:$ 未定义,位置与其他 HTML 同时加载,以及 VPN 的准确性
问题描述
我想在客户端使用 Geolocation API 来识别用户的 IP 并将他们的状态添加到我的网页中。由于我对学习 Javascript 和 jQuery 还很陌生,你能帮我解决下面的问题吗?
我从控制台收到一个未定义 $ 的错误 - 你建议我如何解决这个问题以便解决方案有效?(见脚本接近尾声)
其他问题:
- 我希望用户的状态在出现周围文本的同时呈现——我该如何做到这一点?
- 我用 VPN 测试了 API 并发现了一些不准确之处——当用户使用 VPN 在加利福尼亚时,API 跟踪 IP(并确定其状态)是否比他们实际上在加利福尼亚更难?(以加州为例)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Get User State & Display</title>
<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.1.0/ipgeolocation.min.js"></script>
<script>
_ipgeolocation.enableSessionStorage(true);
var ip = sessionStorage.getItem("ip");
var country_name = sessionStorage.getItem("country_name");
var state_prov = sessionStorage.getItem("state_prov");
if (!ip || !country_name || !country_code2) {
_ipgeolocation.makeAsyncCallsToAPI(false);
_ipgeolocation.setFields("country_name, state_prov");
_ipgeolocation.getGeolocation(handleResponse);
}
function handleResponse(json) {
ip = json.ip;
country_name = json.country_name;
state_prov = json.state_prov;
}
$("document").onload(function() {
$('.state').html(json.state_prov);
});
// I am receiving an error from the Console that $ is not defined – how do you suggest I solve this problem so the solution works?
</script>
</head>
<body>
<p><strong>If you would like to know more about stock options in <span class="state"></span>, contact our team today!</strong></p>
</body>
</html>
解决方案
推荐阅读
- javascript - How to get the first class of the class attribute
- python - Anaconda 连接到从另一台服务器创建的环境
- ip - Terraform Openstack 将预分配的浮动 IP 附加到实例
- sql - 设备组装管理
- c# - 处理身份验证/授权:ASP.NET Core Web 应用程序 => ASP.NET Core Web API => SQL
- java - 没有 @Builder 的 Lombok @Value 的默认值
- java - Base 64 Encoded QRGen 二维码无法解码
- python - 如何拆分字符串列表?
- typescript - 如何避免修改初始对象并改进映射?
- django - 如何使用颤振在 django 中进行 google、facebook 身份验证