Jquery ajax在处理时更新数据(在成功或完成之前)

Jquery ajax在处理时更新数据(在成功或完成之前),jquery,ajax,while-loop,loading,Jquery,Ajax,While Loop,Loading,我试图在处理时更新数据(在成功或完成之前) .ajax的“成功”部分将等待php完成 在大多数情况下,等待“成功”是完全可以的 但对于较长的php(如在大文件上使用ffmpeg),等待时间太长,会产生错误 我有这个密码 $.ajax ({ type: 'GET', async: true, url: '__/_/_path_to_PHP_file.php', dataType: 'html', data:'__POSTvariable1__='+enc

我试图在处理时更新数据(在成功或完成之前)

.ajax的“成功”部分将等待php完成

在大多数情况下,等待“成功”是完全可以的

但对于较长的php(如在大文件上使用ffmpeg),等待时间太长,会产生错误

我有这个密码

$.ajax ({
    type: 'GET', 
    async: true, 
    url: '__/_/_path_to_PHP_file.php',
    dataType: 'html',
    data:'__POSTvariable1__='+encodeURIComponent (__POSTvariable1__)+
         '&__POSTvariable2__='+encodeURIComponent(__POSTvariable2__),
    cache: false,
    success: function(data){
        $('#div_that_needs_update').html(data);
    },
    error: function(request, status, error){
        console.log ( 'request.responseText --> ' + request.responseText + ' status --> ' + status + ' error --> ' + error );
    }
});
••我尝试了“发送前”的“完整”,但是否存在“whileload”或类似的情况


谢谢

我想你在找xhr

$.ajax({
        xhr: function(){
            var xhr = new window.XMLHttpRequest();
            //Upload progress
            xhr.upload.addEventListener("progress", function(evt){
                if (evt.lengthComputable) {
                    var percentComplete = (evt.loaded / evt.total) * 100;
                    $("#status").html(Math.round(percentComplete)); 
                }
            }, false);
            //Upload progress
            xhr.upload.addEventListener("load", function(evt){

            }, false);
            xhr.upload.addEventListener("error", function(evt){
                $("#status").html("Upload Failed");
            }, false);
            xhr.upload.addEventListener("abort", function(evt){
                $("#status").html("Upload Aborted");
            }, false);
           return xhr;
        },
        url: .........
希望对您有所帮助

谢谢您的回答(这是一个非常好的开始)

我找到了这个页面:

我想出来了;)


我最终确定它更简单、更高效(带有参数)


对于更长的php,我想使用flush();ob_flush();回声。。。但这段代码要等到一切都完成了;(嗨,很抱歉,addEventListener不是我要找的。我要找的是一些可以从php获得更多反馈的东西。例如:当php正在处理时,我有很多echo,我想更新每个echo'live',而不用等待整个php一次全部回显。嗨,John..我真的不知道在xhr中调用php文件是一种好方法。打开时,我们已经在ajax url中调用了它…我认为如果您的代码运行良好,它就可以了..但是您也可以观看本教程。。
$.ajax ({
    type: 'GET', 
    async: true, 
    url: '__/_/_path_to_PHP_file.php',
    dataType: 'html',
    data:'__POSTvariable1__='+encodeURIComponent (__POSTvariable1__)+
         '&__POSTvariable2__='+encodeURIComponent(__POSTvariable2__),
    cache: false,
    xhr: function(){
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '__/_/_path_to_PHP_file.php', true);
        xhr.onprogress = function(e) {
           $('#div_that_needs_update').html(e.currentTarget.responseText);
         }
         return xhr;
    },
    success: function(data){
        console.log('completed');
    },
    error: function(request, status, error){
        console.log ( 'request.responseText --> ' + request.responseText + ' status --> ' + status + ' error --> ' + error );
    }
});
function liveXHR (p1 , p2) {
    var params = 'param1='+p1+'&param2='+p2;

    xhr = new XMLHttpRequest();
    xhr.open('GET', '__URL_OF_PHP___'+"?"+params, true);
    xhr.onprogress = function(e) {
        $('#__ID_DIV_forUpdate___').html(e.currentTarget.responseText);
    }
    xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4) { 
            console.log('Complete'); 
            //or any onther stuff when done ;)
        } 
    }
    xhr.send();
};