Moment timezone data js error: "has no data for <!DOCTYPE html>"

Moment timezone data js error: "has no data for <!DOCTYPE html>"

我正在使用设置了 hmvc 功能的 CodeIgniter 3.*。我一直在测试我的应用程序以设置一个使用时区的时钟,该时区使用用户选择的默认时区。这个时钟用的是moment js.

奇怪的是,当我正确设置时钟后,此错误消息会出现在任何未创建 moment 实例的页面上。我不知道为什么会这样。我正在使用 Chrome 来显示我的输出。

我正在使用moment js和moment data cdns:

  <!----moment CDNs ---->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<!----moment timezone CDNs ---->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.js"></script>

当我不调用 moment 进行初始化时弹出此错误:

Moment Timezone has no data for <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Project X - Login</title>
    <!----JQuery CDNs ---->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <!----Bootstrap CDNs ---->
    <link rel="stylesheet" type="text/css" rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"/>
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <!----moment CDNs ---->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
    <!----moment timezone CDNs ---->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-utils.js"></script>
    <!----Daterangepicker CDNs ---->
      <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css"/>
        <script src="https://cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
    <!----Select2 CDNs ---->
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
    <!----D3 CDNs ---->
        <script src="https://www.gstatic.com/charts/loader.js"></script>
        <script src="https://d3js.org/d3.v3.js"></script>
    <!----Tinymce CDNs ---->
    <script src="https://cdn.tinymce.com/4/tinymce.min.js"></script>
    <!----Datatables CDNs ---->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.css">
    <script charset="utf8" src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
    <!----Font Awsome---->
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
    <!----Regular Links ---->
    <link href="http://[::1]/ApplicationProject1/assets/css/style.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="http://[::1]/ApplicationProject1/assets/css/user.css"/>
    <link rel="icon" type="image/jpeg" href="http://[::1]/ApplicationProject1/assets/images/favicon.jpg">
    <script src="http://[::1]/ApplicationProject1/assets/js/user.js"></script>
    <script src="http://[::1]/ApplicationProject1/assets/js/objects.js"></script>
    <script src="http://[::1]/ApplicationProject1/assets/js/default.js"></script>
  </head>
<body>
<div class="container">
    <div class="row">
                        <div class="col-md-12">
            <div class="log-in">
                <div class="page-header">
                    <h1>Login</h1>
                </div>
                <form action="http://[::1]/ApplicationProject1/index.php/user/login/get_timezone" method="post" accept-charset="utf-8">
                <div class="input-group" style="margin:2px;width:99%;">
                        <div class="form-group">
                            <div class="input-group" style="margin-bottom:10px;">
                            <span class="input-group-addon fa fa-user" id="basic-addon1"></span>
                            <input type="text" id="username" name="username" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
                         </div>
                        <div class="form-group" style="margin-bottom:15px;">
                            <div class="input-group">
                                <span class="input-group-addon fa fa-lock" id="basic-addon1"></span>
                                <input type="password" class="form-control" id="password" name="password" placeholder="Password" aria-describedby="basic-addon1">
                            </div>
                        </div>
                            <div class="form-group">
                                <input style="float: left; margin-right: 10px" type="submit" class="btn btn-default" value="Login">
                            </div>
                </div>
                </form>
                <div>
                        <a href="http://[::1]/ApplicationProject1/index.php/user/register"><p class="btn btn-default">New User</p></a>
                </div>
            </div>
        </div>
    </div><!-- .row -->
</div><!-- .container -->
. See http://momentjs.com/timezone/docs/#/data-loading/.

我无法弄清楚为什么它只出现在不调用 moment 的页面上。

这是我从你建议的调试中得到的,Adrian Grzywaczewski:

jquery.min.js:2 jQuery.Deferred exception: deviceTimeZone is not defined ReferenceError: deviceTimeZone is not defined
    at HTMLDocument.<anonymous> (http://[::1]/ApplicationProject1/assets/js/user.js:2:28)
    at j (https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js:2:29999)
    at k (https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js:2:30313) undefined
r.Deferred.exceptionHook @ jquery.min.js:2
k @ jquery.min.js:2
setTimeout (async)
(anonymous) @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
fire @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
ready @ jquery.min.js:2
S @ jquery.min.js:3
jquery.min.js:2 Uncaught ReferenceError: deviceTimeZone is not defined
    at HTMLDocument.<anonymous> (http://[::1]/ApplicationProject1/assets/js/user.js:2)
    at j (jquery.min.js:2)
    at k (jquery.min.js:2)
(anonymous) @ http://[::1]/ApplicationProject1/assets/js/user.js:2
j @ jquery.min.js:2
k @ jquery.min.js:2
setTimeout (async)
r.readyException @ jquery.min.js:2
(anonymous) @ jquery.min.js:2
j @ jquery.min.js:2
k @ jquery.min.js:2
setTimeout (async)
(anonymous) @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
fire @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
k @ jquery.min.js:2
setTimeout (async)
(anonymous) @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
fire @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
ready @ jquery.min.js:2
S @ jquery.min.js:3
jquery.min.js:4 GET http://localhost/ApplicationProject1//get_timezone 404 (Not Found)

正在调用 get_timezone,因为我在所有页面上都调用了时钟,但此时时区未定义,因此提供了默认时区。获取时区的调用失败这一事实是否改变了什么?

-- 已修改 -- 问题是我的 get_timezone 函数由于某种原因正在返回页面。这是函数的代码:

/**
     * default_time_zone function gets the default time zone to use for the modules.
     * @author Peter Kaufman
     */
    public function default_time_zone() {
        $sql = "SELECT `value` FROM `config` WHERE `name` = 'timezone';";
        $results = $this->db->query($sql)->result();

        if (is_array($results)) {
            $results = (array)array_shift($results);
            $_SESSION['timezone'] = $results['value'];
        } else {
            $_SESSION['timezone'] = 'America/New_York';
        }
    }

    /**
     * get_time_zone gets the timezone to use.
     * @author Peter Kaufman
     */
    public function get_timezone() {
        exit($_SESSION['timezone']);
    }

有些地方还有另一个名为 get_timezone 的函数,因为即使 get_timezone 函数的 link 不正确,也能找到它。

答案是添加一个 if 语句以确保仅当页面标题没有注册或登录时才调用获取默认时区。

"use strict";
$(document).ready(function() {
    var title = document.getElementsByTagName("title")[0].innerHTML;
    title = title.replace('Project X - ', '');
    if (title != 'Register' && title != 'Login') {
        $.ajax({
            url: location + '/get_timezone',
            success: function(resp) {
                new Clock(1000, resp);
            },
        });
    }
});