Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX requests on Chrome using jQuery freezes page until they all complete

Code below works well on Firefox - displays progress bar which progresses on every file being uploaded, meanwhile in Chrome, it only displays progress bar at the end of transaction, also when I click "submit" button it freezes up until function completes.

var max = files.length + 1;
var progress_step = 0;
$.post(form.action, $(form).serialize(), function(response){
  var data = jQuery.parseJSON(response);
  if ("errors" in data){
    //...;
  }
  else if ("work_id" in data){
    var work_id = data.work_id;
    //initial increase of progress once Work was created
    progress_step = progress_step + 1;
    progress(progress_step, max);

    $.each(files, function(index, obj){
      uploadFile(work_id, obj);
      progress_step = progress_step + 1;
      progress(progress_step, max);
    });
  }
});

...

function uploadFile (w_id, obj) {    
  var base64_start = obj.src.indexOf(',') + 1;
  $.ajax({
    type: 'POST',
    url: '/works/upload_image',
    data: {work_id: w_id, pic: obj.src.substr(base64_start), pic_type: obj.file.type},
    processData: true,
    timeout: 60000,
    async: false,
    dataType: 'text'
  }); 
}
like image 201
spacemonkey Avatar asked Mar 03 '11 15:03

spacemonkey


People also ask

Is AJAX better than JQuery?

While JQuery is a library for better client-side web page development, AJAX is a technique of doing XMLHttpRequest to the server from the web page and sending/retrieving data used on a web page. AJAX can change data without reloading the web page. In other words, it implements partial server requests.

Do AJAX requests get cached?

Although we can use a standard caching solution provided by HTTP (yes, Ajax is cached by HTTP), there is a catch: It works for GET requests only (not POST).

What is the default timeout for JQuery AJAX?

The default value is 0 , which means there is no timeout.

How do I know if AJAX request is successful?

You can check when exactly it returns "success" : // If successful, handle type chaining if ( status >= 200 && status < 300 || status === 304 ) { ... // If not modified if ( status === 304 ) { statusText = "notmodified"; ... // If we have data } else { try { ...


2 Answers

If you don't want it to freeze, change async: false to async: true. Since this will make an asynchronous request, it might break the functionality of your progress bar, because it will move on to the next line of code before the request has completed. To fix this, use

success: function() { /*code in here*/ }

to put the code you want to activate when the request is finished.

like image 130
Peter Olson Avatar answered Sep 28 '22 09:09

Peter Olson


Using async: false will freeze the page until the server replies.
Do not use it.

Get rid of that line and it will work fine.
However, it will be asynchronous, so you'll need to move your loop into the success callback.

like image 24
SLaks Avatar answered Sep 28 '22 10:09

SLaks