Javascript XMLHttpRequest onprogress未捕获语法错误:无效或意外标记

Javascript XMLHttpRequest onprogress未捕获语法错误:无效或意外标记,javascript,ajax,xmlhttprequest,live,Javascript,Ajax,Xmlhttprequest,Live,我有一个工作的ajax函数 但当我使用onprogress时,有时会返回一半的html,控制台会显示未捕获的SyntaxError:Invalid或unexpected token,但我还是继续 这里是函数 function xhrAJAX ( divID , param2 ) { var pcache = (Math.floor(Math.random() * 100000000) + 1); var params = "divID="+encodeURI

我有一个工作的ajax函数

但当我使用onprogress时,有时会返回一半的html,控制台会显示
未捕获的SyntaxError:Invalid或unexpected token
,但我还是继续

这里是函数

function xhrAJAX ( divID , param2 ) {

    var pcache = (Math.floor(Math.random() * 100000000) + 1);
    var params = "divID="+encodeURIComponent(divID)+"&param2="+encodeURIComponent(param2);
    var xhr = new XMLHttpRequest(); xhr.open("POST", "/file.php?pcache="+pcache, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onprogress = function(e) { $("#"+divID).html(e.currentTarget.responseText) ; }
    xhr.send(params);

}
*****我知道如果我用
xhr.onreadystatechange=function(e){If(xhr.readyState==4){$(“#”+divID).html(e.currentTarget.responseText);}}
更改.onprogress,我就不会出现控制台错误。。。。但是有时候,php中发生的事情需要很长时间才能执行,并且需要显示进度

我的PHP8配置了NGINX,没有输出缓冲区,所以如果我使用
echo'stuff';睡眠(3);回声“2”我看到“stuff”,然后在3秒钟后看到“2”出现

请注意,这里描述的php部分不是问题所在


问题是:有没有一种方法可以避免onprogress(在javascript端)上出现“半返回的html”

我找到了新的javascript替换获取。通过一些挖掘,我想出了这个可流式html

它还可以在webviews(android和IOS)中使用;)

在这个场景中,我有关闭输出缓冲区的PHP8和NGINX,因此在继续执行时会推送每个回显

function fetchsrteam ( divid , sub , buttonid ) {
    
    var pcache = (Math.floor(Math.random() * 100000000) + 1); 
    var postix = [];
    postix["preventCache"] = pcache;
    postix["divid"] = encodeURIComponent(divid);
    postix["buttonid"] = encodeURIComponent(buttonid);
        
    fetch("/.........php?pcache="+pcache, {
      method: "POST",
      body: JSON.stringify(Object.assign({}, postix)),
      headers: {"Content-type": "application/json; charset=UTF-8"}
    }).then(response => response.body)
      .then(rb => {
        const reader = rb.getReader();
          return new ReadableStream({
            start(controller) {
              function push() {
                reader.read().then( ({done, value}) => {
                  if (done) {
                    console.log('done', done); controller.close(); return;
                  }
                  controller.enqueue(value); $("#"+divid).append(new TextDecoder().decode(value)); push();
                })
              }
            push();
            }
          });
    });

}

否。
responseText
是原始文本缓冲区。不要使用任意html进行流式处理。把它分成几行,每行都是有效的html或类似的东西。@Bergi我刚刚明白了你的意思,进一步挖掘我发现了这个“[违规]在执行JavaScript时强制回流需要36毫秒”,所以这是一毫秒thing@Bergi是否有一种javascript方法可以“过滤”或“堆栈”呢缓冲区而不是同时输出所有接收到的缓冲区?如我所说,使用类似于
responseText.split('\n')
的方法,并显示除最后一行(不完整)之外的所有行。@Bergi您知道是否有任何方法可以避免“执行时回流”javascript是否有办法检测到这一点?