Page reload with cacheless Jquery (Chrome)

Asked

Viewed 667 times

0

I don’t really know if the problem is the cache or the script I made or the server I’m using, but come on.

I’m using this code to upload a document to the server(I haven’t evaluated security and performance issues yet).

if($("#add-doc").isValid()){

$.ajax({
    url: 'api/upload-doc.php',
	type: 'POST',
	data: formData,
	beforeSend: function(){

		$("#docs-objeto3 #forms").hide();
		$("#docs-objeto3 #loader-bar").show().fadeIn('slow');

	},
	success: function (data) {
		console.log(data);
	},
	cache: false,
	contentType: false,
	processData: false
	xhr: function() {  // Custom XMLHttpRequest
		            
		var myXhr = $.ajaxSettings.xhr();
		            
		if (myXhr.upload) { // Avalia se tem suporte a propriedade upload
		myXhr.upload.addEventListener('progress', function (evt) {
		     if (evt.lengthComputable) {
					            
				var percentComplete = evt.loaded / evt.total;

				var porcentagem = percentComplete * 100;

				console.log(parseInt(porcentagem)+'%');

				$('#docs-objeto3 #loader-bar .loadbar-label').text(parseInt(porcentagem)+'%');
				$('#docs-objeto3 #loader-bar .progress-bar').width(parseInt(porcentagem)+'%');

				if(porcentagem == 100){
					            	
					// window.location.reload(true);
					location.reload(true);
					// document.location.href = 'detalhe-objeto.php?objeto_id='+objeto_id+'&rt=success';
					// $(location).attr('href', 'detalhe-objeto.php?objeto_id='+objeto_id+'&rt=success')

				}

			}
		}, false);
	}
	return myXhr;

	},
	close: function(){

		$("#docs-objeto3 #forms").show();
		$("#docs-objeto3 #loader-bar").hide();

	}
});

}

Note that I am using the method location.reload(true); which in Firefox works perfectly, locally works in Chrome and firefox, but when uploading to the server does not work properly in Chrome. I tested other ways as can be seen commented, always the same result.

When the page is reloaded the new content that was inserted does not present, having to give F5 once again for the data to appear on the screen, I am not loading this content via ajax, it is loaded via php normal on the page. And as the content does not load the form that should disappear, it is still active enabling the sending of more documents, even if the sending of more than one documents is blocked this problem passes to the user that the doc was not sent. Since the information does not appear on the screen.

  • Does the problem only occur in Chrome? Already tested by anonymous browsing?

  • Just in Chrome, I’ve tried anonymous too, same result. I’m suspecting it might be server-side, because local works perfectly. Anyway I’ll open a call on the server.

  • Reload doesn’t just happen when I send a pdf. :(

  • In these cases it is very important to follow the browser console to debug the javascript and the server response to get a clearer sense of what might be happening,

2 answers

0


For some reason that I do not know (if anyone knows), the headers of the called file were not being processed before the redirect, I found that sent a larger file and redirected normal. So I added the following line that still needs to be optimized.

setTimeout(function(){	            		
  location.reload(true);
}, 3000); //Tempo 3 segundos antes de redirecionar.

0

Is it the best solution? NO. But you can try to use for testing:

window.location.href = window.location.href;

In my opinion it is a kind of dirty way, but for testing in the browsers it works.

  • I used it instead of location.reload(true), of the same result above. What worked was using the setTimeout() regardless of the way the redirect is being done..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.