XMLHttpRequest chunked response, only read last response in progress

前端 未结 1 1488
盖世英雄少女心
盖世英雄少女心 2021-01-18 06:42

I\'m sending chunked data from a NodeJS application back to the browser. The chunks are really json strings. Problem I\'m having is that every time the onprogress

相关标签:
1条回答
  • 2021-01-18 07:16

    This index based approach works for me:

    var last_index = 0;
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "/servers/scan");
    xhr.onprogress = function () {
        var curr_index = xhr.responseText.length;
        if (last_index == curr_index) return; 
        var s = xhr.responseText.substring(last_index, curr_index);
        last_index = curr_index;
        console.log("PROGRESS:", s);
    };
    xhr.send();
    

    Inspired by https://friendlybit.com/js/partial-xmlhttprequest-responses/

    0 讨论(0)
提交回复
热议问题