简体   繁体   English

ajax调用何时完全完成?

[英]When the ajax call is fully completed?

when any ajax call is done by clients , Clients uploads data to server(HTTP request) and then receive data from server(HTTP Response), when the data is received totally by clients, the ajax request is consider successful and success callback function is executed , 当客户端完成任何ajax调用时,客户端会将数据上传到服务器(HTTP请求),然后从服务器接收数据(HTTP响应),当客户端完全接收到数据时,该ajax请求被视为成功并且执行成功回调函数,

But when i try to track ,this data transfer between client and server , by using xhr in $.ajax call 但是当我尝试跟踪时,通过在$.ajax调用中使用xhr来进行客户端和服务器之间的数据传输

xhr: function(){

    var xhr = $.ajaxSettings.xhr() ;

    xhr.upload.onprogress = function(e){console.log('upload progress',e.loaded/e.total*100) } ;
     xhr.onprogress =  function(e){console.log('total downloaded',e.loaded)}
    return xhr ;

In this ajax request , server is sending 277695 bytes file , But The data is not completely downloaded, still it says XHR finished loading , Why after some part is downloaded first , And XHR request is become complerted , 在此ajax请求中,服务器正在发送277695字节文件,但是数据没有完全下载,还说XHR已完成加载,为什么先下载了一部分后,又XHR请求变得兼容了,

在此处输入图片说明

We can see here server a is sending a file of length 277695 bytes , But only the 7946 bytes downloaded , and it says xhr finished loading , I was expecting , all file (277695 bytes)should be downloaded before XHR finsihed loading. 我们在这里可以看到服务器a正在发送一个长度为277695字节的文件,但是仅下载了7946字节,并且它说xhr已完成加载,我期望在XHR完成加载之前应下载所有文件(277695字节)。 So can someone explain why only some part of file is downloaded first ? 那么,有人可以解释为什么只首先下载文件的一部分吗?

UPDATE UPDATE

And i also want to ask when the success call back function is executed, because it will be executed when whole result(response) received from server . 我也想问一下何时执行成功回调函数,因为它将在从server接收到整个结果(响应)时执行。 So when the success callback function will be executed ? 那么何时成功执行回调函数? after total download is 7946, OR after the whole data downloaded , ie total downloaded is 277695 总下载后为7946,或者全部数据下载后,即总下载为277695

The log XHR finished loading... is made by the network monitoring that is part of your console. 日志XHR finished loading...是由控制台中的网络监视完成的。 The point is that this log is made almost at the same time that the true complete transfer has happened, not immediately(!) after the 7946 bytes were read. 关键是该日志几乎是在真正完整的传输发生的同时进行的,而不是在读取7946个字节后立即进行的! In short, it is OK like it is. 简而言之,就可以了。 Just use the load event, which triggers immediately(!) after you get the XHR finished message. 只需使用load事件,该事件会在您收到XHR完成消息后立即触发(!)。

Use the XHR "load" event listener: 使用XHR“加载”事件监听器:

 xhr.addEventListener("load", transferCompleteCallback);

Read more: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest 阅读更多: https : //developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM