4
I’m using jqGrid 4.54 in my project and I want to put a message blocking the whole screen with the plugin blockUI when my AJAX request encounters some server error like 500 error.
I know that the blockUI 2.66.0 does not work with synchronous AJAX, so I am using my plugin jqGrid to make your AJAX requests as below:
$.extend($.jgrid.ajaxOptions, { async: true });
$.extend($.jgrid.defaults, {
mtype: "POST",
altRows: true,
datatype: "json",
loadonce: true,
height: "auto",
width: 1100,
rowNum: 10,
rowList: [10, 20, 30, 40, 50],
viewrecords: true,
pager: "#paginacao",
sortorder: "asc",
shrinkToFit: false,
headertitles: true,
loadui: "disable",
rownumbers: true,
emptyrecords: "<strong>Não houve resultado para o seu filtro.<strong>",
autoencode: true,
caption: "Resultados encontrados",
deselectAfterSort: true,
gridview: true,
idPrefix: "id",
rowTotal: 4000,
sortable: true,
toppager: true,
loadError: function(xhr, status, error) {
$.blockUI({
message: '<p style=\"font-weight: bolder; color: white;\">Erro ao tentar gerar relatório, por favor, tente novamente.<br /><br /><a onclick=\"$.unblockUI();\">Fechar</a></p>',
timeout: 5000,
onOverlayClick: $.unblockUI
});
},
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: true,
id: 0,
cell: ""
}
});
But doing so the plugin does not work at all. But when I play the call to blockUI within a window.setTimeout as below works perfectly:
loadError: function(xhr, status, error) {
window.setTimeout("$.blockUI({ message: '<p style=\"font-weight: bolder; color: white;\">Erro ao tentar gerar relatório, por favor, tente novamente.<br /><br /><a onclick=\"$.unblockUI();\">Fechar</a></p>', timeout: 5000, onOverlayClick: $.unblockUI});", 10);
}
Does anyone know how to make the call to blockUI be executed without having to use it within a window.setTimeout
?
In time, all Javascript native functions such as parseInt
, parseFloat
, alert
, console.log
work, which leads me to believe that the problem is like the blockUI
handles Ajax, the problem I use it asynchronously as it requires and it doesn’t even work without the window.setTimeout
.
EDIT: According to the utluiz response, there may be some conflict with my default settings of how I handle AJAX requests, follow the configuration:
$.ajaxSetup({
type: "POST",
dataType : "json",
cache : false,
error : function(xhr, statusRequestAjax, error) { $("#msgErros").html(error); },
beforeSend: function() { $.blockUI(); },
complete : function() { $.unblockUI(); }
});
I had already thought about this part of removing parts of the system to isolate the problem, but this will simply be a hell to do, because there is no point in saving the HTML because it will not execute the ajax request I need to isolate the problem. As for blockUI having problems with AJAX is not an assumption, it is a certainty. I copied and pasted an AJAX call via jQuery that was set to be synchronous and the plugin did not work, even because synchronous AJAX already blocks the screen by itself, because nothing can run before the end of it.
– Philippe Gioseffi
Some links on the subject: http://stackoverflow.com/questions/6009223/blockui-vs-ajax-with-async-option-to-false and http://stackoverflow.com/questions/2875074/jquery-blockui-blocking-message-does-not-immediately-show
– Philippe Gioseffi
@Philippe Again: the problem is not related to Ajax. Both posts talk about the issue of the browser being locked during a synchronous Ajax. This is not about Blockui, actually, nothingness would work in that situation. The problem in such cases is that the developer does not take into account the correct sequence of events. It is possible to use a Blockui during a synchronous Ajax if Ajax is executed *after the screen is completely locked.
– utluiz
I made an example that might be seen here with Blockui both during and after Ajax. If you change the
async
forfalse
works the same way, only that there is a brief visual "locking" due to browser locking.– utluiz
@Philippe See this other example using Synchronous Ajax: http://jsfiddle.net/jL2tJ/
– utluiz
I was confused now, my solution to set up ajax to be asynchronous worked perfectly before where blockUI didn’t work and it seemed to me to be that problem, you now said and showed code that this is not the problem. The second link I sent you has an updated response that led me to the conclusion I had.
– Philippe Gioseffi
Follow the passage: "I don’t know if this is still Relevant, but in the end I Managed to make it work. The problem was that I was Doing a synchronous call with ajax (the "async: false" bit in the submitForm call). Blockui is thought to make an asynchronous call work as if it was synchronous, i.e. blocking the interface. If the call is already synchronous there is no need to block the ui, because that’s the normal behavior of synchronous calls!"
– Philippe Gioseffi
All this excerpt says is that it is not necessary to use Blockui in synchronous requests.
– utluiz
That’s what it really says. Now the funny thing is the native functions of javascript work and even other libraries, except the blockUI.
– Philippe Gioseffi
@Philippe Unfortunately this seems to be a particular situation. Without playing there is no help. You have tried in another browser?
– utluiz
The behavior is repeated in both Chrome and Firefox using the last two versions of them. I’ll start editing the HTML even deleting and including things. Maybe it’s something with my AJAX request settings made by us. I’ll edit to ask and put our setup here, rereading your answer I think it might have some impact.
– Philippe Gioseffi
you were correct, my ajax configuration was conflicting with the jqGrid plugin. We have a jQuery plugin here from the project, so I encapsulated the settings in a method. I will post as an answer, but I will mark yours as an answer, because yours led us to the correct answer.
– Philippe Gioseffi
@Philippe Ufa! It was nice to help.
– utluiz
Thanks, @utluiz. If possible opinion on the solution adopted.
– Philippe Gioseffi