$.get函数不在Chrome中运行,但在IE和FireFox中运行

$.get function not running in Chrome but working in IE and FireFox

本文关键字:运行 但在 IE FireFox get Chrome 函数      更新时间:2023-09-26

我在一个使用phpPOST将文件上传到服务器的网站上工作,我正在尝试为上传添加进度条。我遵循了这个指南:

http://www.ultramegatech.com/2010/10/create-an-upload-progress-bar-with-php-and-jquery/

它在ie和firefox中效果很好。但是进度条从未在chrome中更新。

调用此函数时超时值为"500"。

function updateProgress(id) {
    var time = new Date().getTime();
    // Make a GET request to the server
    // Pass our upload identifier as a parameter
    // Also pass current time to prevent caching
    $.get('progressbar.php', { uid: id, t: time }, function (data) {
        // Get the output as an integer
        var progress = parseInt(data, 10);
        if (progress < 100 || !started) {
            var div = document.getElementById('statusfield');
            div.innerHTML = progress + '%';
            // Determine if upload has started
            started = progress < 100;
            // If we aren't done or started, update again
            updateProgress(id);
        }
        if (progress > 99) {
            var div = document.getElementById('statusfield');
            div.innerHTML = 'Komprimerar fil...';
        }
        // Update the progress bar percentage
        // But only if we have started
        started && pbar.progressbar('value', progress);
    });
}

此函数调用.php文件"progressbar.php",该文件将上传进度以百分比形式返回。

progressbar.php:

<?php
   if (isset($_GET['uid'])) {
        // Fetch the upload progress data
        $status = uploadprogress_get_info($_GET['uid']);
        if ($status) {
            // Calculate the current percentage
            echo round($status['bytes_uploaded']/$status['bytes_total']*100);
        }
        else {
            // If there is no data, assume it's done
            echo 100;
        } 
    }
?>

我已经在chrome中测试了代码,并调用了函数"updateProgress"。但它从未通过:

$.get('progressbar.php', { uid: id, t: time }, function

有人知道可能出了什么问题吗?

谢谢!

在chrome中,转到开发工具(选项->工具->开发工具)并查看网络面板。一旦调用$.get方法,您将看到您的请求和结果-您可以看到它是否失败(例如,如果发生404),因此chrome可能没有按应有的方式设置地址,或者发送的数据/返回的数据是否不正常。

相关文章: