首页 > 解决方案 > 地理位置 API 问题:$ 未定义,位置与其他 HTML 同时加载,以及 VPN 的准确性

问题描述

我想在客户端使用 Geolocation API 来识别用户的 IP 并将他们的状态添加到我的网页中。由于我对学习 Javascript 和 jQuery 还很陌生,你能帮我解决下面的问题吗?

我从控制台收到一个未定义 $ 的错误 - 你建议我如何解决这个问题以便解决方案有效?(见脚本接近尾声)

其他问题:

<!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>

标签: javascripthtmljqueryapigeolocation

解决方案


推荐阅读