jQuery进度条跳到100%

时间:2016-04-24 20:04:49

标签: javascript php jquery

我有这个设置:

  

HTML

 <div id="tst_loader"></div>
  

JAVASCRIPT

$.ajax({
    xhr: function() {
        var xhr = new window.XMLHttpRequest();
        xhr.upload.addEventListener("progress", function(evt) {
            if (evt.lengthComputable) {
                var percentComplete = evt.loaded / evt.total;
                //Do something with upload progress here
        $('#tst_loader').html(percentComplete * 100 + '%');
            }
       }, false);

       xhr.addEventListener("progress", function(evt) {
           if (evt.lengthComputable) {
               var percentComplete = evt.loaded / evt.total;
               //Do something with download progress
            $('#tst_loader').html(percentComplete * 100 + '%');
           }
       }, false);

       return xhr;
    },
    type: 'POST',
    url: url,
    data: {feed:""},
    success: function(data){
        //Do something on success

     alert('done');
    }
  

PHP代码:

<?php

$_POST['feed'] = 'http://www.cs.washington.edu/research/xmldatasets/data/tpc-h/orders.xml';/// A little heavier file for testing


                $dom = new DOMDocument; 
                $dom->load($_POST['feed']);

if(false !== $dom){

$use_errors = libxml_use_internal_errors(true); 
                        $s = simplexml_import_dom($dom);
                        libxml_clear_errors();
                        libxml_use_internal_errors($use_errors);
                        //libxml_disable_entity_loader( $oldDisable );

                        if(false !==    $s){    
$ctr = 0;
foreach ($s->channel->item as $item) {

  $out[$ctr]=$item->title;

$ctr++; 
}

echo json_encode( $out);

}


}




        ?>

每当代码运行时,即使progressbar仍在被提取,100%也会立即跳转到RSS FEED ....换句话说,没有显示进度。一切看起来都很棒,但它一直在跳......任何想法如何实现这一点?? ...谢谢!

编辑::将链接更改为更重的内容:http://www.cs.washington.edu/research/xmldatasets/data/tpc-h/orders.xml

1 个答案:

答案 0 :(得分:0)

正如问题评论中所讨论的那样,设置Content-Length标题的方式(虽然在这种情况下它没有那么有用)就是用这个替换你的echo json_encode( $out);

$content = json_encode($out);
header("Content-Length: ".strlen($content));
echo $content;