0

I call a php page using an ajax request, then the php page calls a python script and the python script writes percentage of work done in a txt file and of course calculates what is needed.

From my code it is only possible to acess the txt file after the ajax request is done but this doesn't make sense as it is always 100%.

How would i read data if the txt with the progress is lets say: http://domain.com/progress.txt durring the request.

$.ajax({
xhr: function() {
        var xhr = new XMLHttpRequest();
  xhr.open('GET', "http://domain.com/pr.txt", true);
  xhr.send();
  var pro_perc = xhr.responseText;;
  alert(pro_perc);
  move1(pro_perc);
       return xhr;
    },
type: "POST",
url: "http://domain.com/req.php",
data: reqdata,
cache: false,
success: function(html) {

var values = html.split('[mydata]');
var mydata = values[1];

});
Brana
  • 1,197
  • 3
  • 17
  • 38

1 Answers1

1

Actually you can find your answer here JQuery ajax progress via xhr

or you can use this

var interval;
   $.ajax({
        beforeSend: function(){
            interval = setInterval(function(){
                    $.get("http://domain.com/pr.txt").done(function(progress){
                        console.log(progress)
                    })
                },10);
        },
        type: "POST",
        url: "http://domain.com/req.php",
        data: reqdata,
        cache: false,
        success: function(html) {
            clearInterval(interval);
            var values = html.split('[mydata]');
            var mydata = values[1];
        }
   });
Community
  • 1
  • 1
ArtemSky
  • 1,173
  • 11
  • 20
  • Thanks, i read some answers but didn't get what is actually "evt". I need to read from a txt file using a XMLHttpRequest how to make this an event I do not kow ... – Brana Apr 25 '16 at 16:46
  • 1
    evt its event object, you can push it to console.log() and look at this object. Actually you are reading txt file byte by byte, and evt.total = file size, and evt.loaded its current size – ArtemSky Apr 25 '16 at 16:55
  • The post request doesn't read any file ... it processes the text I sent via post request - it is not a upload progress bar upload problem. – Brana Apr 25 '16 at 17:01
  • Anyways, please check the code I added the hrh part but it seem not to read my file - line: xhr.responseText – Brana Apr 25 '16 at 17:03
  • It is working but it is an infinite loop and it should be stopped after the success: response is received. – Brana Apr 25 '16 at 17:58
  • Actually i saw you added clearInterval(interval) and now it works perfectly. – Brana Apr 25 '16 at 18:03
  • You should have added $.ajaxSetup({ cache: false }); before the code. Would have saved me good 2 hours it took to figure out that the requests were cached :) – Brana Apr 26 '16 at 02:23