Accessing partial response using AJAX or WebSockets?

后端 未结 1 1585
[愿得一人]
[愿得一人] 2020-12-03 15:41

I\'m using some client-side JavaScript code to pull a lot of JSON data from a webserver via an HTTP GET. The amount of data can be big, say 50 MB. This is on a LAN so it\'s

相关标签:
1条回答
  • 2020-12-03 16:37

    Wanted to post this as a comment but the comment textarea is a bit restrictive

    Does the server send the data in chunks at the moment or is it one continuous stream? If you add a handler to the xmlHttpRequestInstance.onreadystatechange how often does it get triggered? If the xmlHttpRequestInstance.readystate has a value of 3 then you can get the current value of the xmlHttpRequestInstance.responseText and keep a track of the length. The next time the readystate changes you can get the most recent data by getting all new data starting at that point.

    I've not tested the following but hopefully it's clear enough:

    var xhr = new XMLHttpRequest();
    var lastPos = 0;
    xhr.onreadystatechange = function() {
      if(xhr.readystate === 3) {
        var data = xhr.responseText.substring(lastPos);
        lastPos = xhr.responseText.length;
    
        process(data);
      } 
    };
    // then of course do `open` and `send` :)
    

    This of course relies on the onreadystatechange event firing. This will work in IE, Chrome, Safari and Firefox.

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