首页 文章

responseText为空,但在控制台上显示?

提问于
浏览
6

这可能是一个愚蠢的错误,但我正在使用JQuery ajax来对付PHP后端,我希望PHP脚本将进度发布回网页 .

它看起来工作正常,但我无法获得e.target.responseText的内容 .

如果我执行console.log(e.target),我会在控制台中获取XMLHttpRequest对象 . 我可以看到responseText:“1 av 1500 linjer”

但是,如果我执行console.log(e.target.responseText),它就是空的 .

我迷失了吗?

这是我的功能:

$.ajax(
    {
        type: "POST",
        url: "modEcs/ajax/ecs.php?a=analyseExcel",
        processData: false,
        contentType: false,
        xhrFields: 
        {
        onprogress: function(e) 
        {               
            console.log(e.target);
            console.log(e.target.responseText);  
            }
        },
        success: function(data) 
        {

         },
         error: function (xhr, ajaxOptions, thrownError) { alert(xhr.statusText); }
    });

控制台中的数据:

XMLHttpRequest
onabort: null
onerror: null
onload: null
onloadend: null
onloadstart: null
onprogress: function(e)
onreadystatechange: null
ontimeout: null
readyState: 4
response: "1 av 1500 linjer"
responseText: "1 av 1500 linjer"
responseType: ""
responseURL: "xxx"
responseXML: null
status: 200
statusText: "OK"
timeout: 0
upload: XMLHttpRequestUpload {onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, …}
withCredentials: false
XMLHttpRequest-prototype

2 回答

  • 0

    仅在XMLHTTPRequest期间触发进度,而不是在收到响应时结束

    readyState 3: "Downloading; responseText holds partial data."
    
  • 1

    试试这个,

    $.ajax({
         type: "POST",
         url: "modEcs/ajax/ecs.php?a=analyseExcel",
         processData: false,
         contentType: false,
         xhr: function(){
            var xhr = new window.XMLHttpRequest();
            // Handle progress
            xhr.addEventListener("progress", function(evt){
            //Do some progress calculations
              console.log(xhr.responseText);
         }, false);
    
       return xhr;
      },
      complete:function(){
        console.log("Request finished.");
      }
    });
    

相关问题