【问题标题】:Chrome Loading an Old Version of Javascript FileChrome 加载旧版本的 Javascript 文件
【发布时间】:2015-05-30 16:01:20
【问题描述】:

我正在使用 CodeIgniter 构建一个 Web 应用程序。

我遇到的问题是 chrome 正在加载我的 javascript 文件 main.js 的旧版本

我的代码:

$(document).load(function(){

/******************************************
*                                         *
*              AJAX FUNCTIONS             *
*                                         *
******************************************/    

function deactivate_dept_member(user_id, token_name, token_hash, callback)
{
    $.post(
        site_url('/ajax/deactivate_dept_member'),
        {
            'user_id':usr_id,
            token_name:token_hash
        },

        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );
}

function activate_dept_member(usr_id, token_name, token_hash, callback)
{
    $.post(
        site_url('/ajax/activate_dept_member'),
        {
            'user_id':usr_id,
            token_name:token_hash
        },

        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );
}

function get_dept_users_for_session(dept_id, token_name, token_hash, callback)
{
    $.post(
        site_url('ajax/get_dept_users_for_session'),
        {
            "dept_id":dept_id,
            token_name:token_hash

        },

        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );

}

function invite_dept_user(email_address, first_name, last_name, admin, token_name, token_hash, callback)
{
    $.post(
        site_url('ajax/invite_dept_user'),
        {
            "email_address":email_address,
            "first_name":first_name,
            "last_name":last_name,
            "admin":admin,
            token_name:token_hash
        },
        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );
}

function get_department_members(dept_id, token_name, token_hash, callback)
{
    $.post(
        site_url('ajax/det_department_members'),
        {
            "dept_id":dept_id,
            token_name:token_hash
        },
        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );
}




/******************************************
*                                         *
*              UTILITY FUNCTIONS          *
*                                         *
******************************************/
function site_url(add_on)
{
    return 'http://localhost:8080/app/index.php'+add_on;
}

function getUserTypeString(user_status)
{
    switch(user_status)
    {
        case "1":
            return "Invited";
        case "2":
            return "Active";
        case "3":
            return "Inactive";
        default:
            return "";
    }
}


function isDepartmentAdmin(user_role)
{
    if(user_role == 2){
        return true;
    }
    return false;
}

function get_error_message(error_code)
{
    switch(error_code)
    {
        case "20130":
            return 'The username already exists';
        default:
            return 'A general error occured';
    }
}


})

Chrome 说的是我的来源:

/******************************************
*                                         *
*              AJAX FUNCTIONS             *
*                                         *
******************************************/    

function deactivate_dept_member(user_id, token_name, token_hash, callback)
{
    $.post(
        site_url('/ajax/deactivate_dept_member'),
        {
            'user_id':usr_id,
            token_name:token_hash
        },

        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );
}

function activate_dept_member(usr_id, token_name, token_hash, callback)
{
    $.post(
        site_url('/ajax/activate_dept_member'),
        {
            'user_id':usr_id,
            token_name:token_hash
        },

        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );
}

function get_dept_users_for_session(dept_id, token_name, token_hash, callback)
{
    $.post(
        site_url('ajax/get_dept_users_for_session'),
        {
            "dept_id":dept_id,
            token_name:token_hash

        },

        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );

}

public function invite_dept_user(email_address, first_name, last_name, admin, token_name, token_hash, callback)
{
    $.post(
        site_url('ajax/invite_dept_user'),
        {
            "email_address":email_address,
            "first_name":first_name,
            "last_name":last_name,
            "admin":admin,
            token_name:token_hash
        },
        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );
}

public function get_department_members(dept_id, token_name, token_hash, callback)
{
    $.post(
        site_url('ajax/det_department_members'),
        {
            "dept_id":dept_id,
            token_name:token_hash
        },
        function(result){
            var response = jQuery.parseJSON(result);
            callback(response);
        }
    );
}




/******************************************
*                                         *
*              UTILITY FUNCTIONS          *
*                                         *
******************************************/
function site_url(add_on)
{
    return 'http://localhost:8080/app/index.php'+add_on;
}

function getUserTypeString(user_status)
{
    switch(user_status)
    {
        case "1":
            return "Invited";
        case "2":
            return "Active";
        case "3":
            return "Inactive";
        default:
            return "";
    }
}

function isDepartmentAdmin(user_role)
{
    if(user_role == 2){
        return true;
    }
    return false;
}

function get_error_message(error_code)
{
    switch(error_code)
    {
        case "20130":
            return 'The username already exists';
        default:
            return 'A general error occured';
    }
}


 �����������������

我为尝试解决问题所做的事情

  • 使用 Chrome 开发者工具禁用缓存
  • 使用 LiveReload 扩展/应用程序
  • 重新启动 Apache
  • 已验证 Vagrant 的共享工作正常(该文件在我运行的虚拟 centos 实例中与在我的 Mac 上的文件相同)
  • 试图让 chrome 加载不同版本的文件(在文件的 url 末尾添加 ?2:localhost:8080/app/assets/js/main.js?2)

其他注意事项:

  • 这也发生在 Firefox 和 Safari 中,因此问题可能出在 Apache 上,但我无法测试这个理论,因为我无法获得 php 的 oci8 扩展来为任何其他操作系统编译。

编辑:

我尝试过的更多东西

  • 重命名文件(会工作一点,但问题又开始出现了。)

【问题讨论】:

  • 您是否尝试过以隐身模式查看网站?
  • 虽然随机,你的文件名(大小写)是一样的吗?记住在 Windows 中 Main 和 main 是相同的,但在 Linux 中不同。您是否也尝试过清理缓存,而不仅仅是禁用它?
  • 在隐身模式下也会发生同样的事情,情况也一样。我在禁用它之前清理了缓存。
  • 也许可以尝试重命名文件并更新代码中的链接(可能是main_.js)。有点老套,但至少可以解决一些问题。
  • 我试过了,该文件曾经被命名为 app.js,更新后提到了这一点。

标签: php javascript apache google-chrome


【解决方案1】:

如果它在重命名时起作用,然后停止工作,那么它不可能是任何类型的缓存(除了可能是“严重损坏的缓存”),也不是文件修改的东西(例如 BOM、流氓 CR 等)。

但我猜该文件可以工作,然后 它停止工作 不是“一段时间后”而是“在你再次保存它之后,做出看起来非常微小且不相关的更改” .

如果是这种情况,您可能正在处理流氓 CR 或奇怪的 EOF 处理:尝试使用其他编辑器(理想情况下为十六进制编辑器)检查磁盘上的文件,和/或使用 最简单 你有文本编辑器。

如果你有一个裸 CR 作为文件的最后一个字符,一些编辑器和平台会在你每次读/写文件时添加两个“奇怪的字符”(实际上是另一个 LF 和另一个流氓 CR)。并且那些相同的编辑器不会向您显示这些字符,因此您将无法从这些编辑器中删除它们。

更新: 另一种明显的可能性是您有一个未正确检测到的“隐藏”UTF-8 字符(或其片段)。然后会发生类似这样的事情:

Visual Studio 2008 project file does not load because of an unexpected encoding change

...请注意,您得到的奇怪字符正是 编码错误 的 UTF8 表示,这也可以解释为什么 Chrome 会检测到 unexpected输入结束

尝试从最后几行代码到文件末尾盲目选择并删除选择,然后将其写回:

            return 'A general error occurred';
        }
    }
})
/* END OF FILE */

并保存。这应该清除事物。如果出现任何奇怪的字符,请从“发生”开始使用十六进制编辑器进行检查。

【讨论】:

  • 我认为这已经接近正在发生的事情的核心。我在十六进制编辑器中查看了该文件,但似乎没有任何恶意回车或文件结尾标记。但是现在在尝试加载该文件时,我在 chrome 中收到“输入意外结束”错误
  • 我现在确信你的编辑对你不忠,我相信我也知道怎么做。编辑答案。
  • 嗯,这发生在我 Mac 上的所有文本编辑器、sublime text、vim 甚至 textedit 中。 Textedit 似乎是唯一没有破坏它的,但它在文件末尾添加了一个无效字符。
  • 越来越奇怪了。您是否有可用的终端实用程序“十六进制”(我不熟悉 MacOS)?如果是这样,请尝试发出命令 hex yourjavascriptfile.js | grep -A2 -B2 " [89a-f][0-9a-f] " 并查看它是否吐出任何非 ASCII 字符。它还会在罪魁祸首之后和之前显示两行,以便您更好地定位它。
  • 我删除了文件并完全重写了它,但问题仍然存在。现在在 chrome 中查看 main.js 时,我确实在文件末尾看到了一堆�字符,但在十六进制编辑器或记事本++中查看文件时,我看不到这些字符。文件中间的大部分内容也丢失了。
猜你喜欢
  • 1970-01-01
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多