I am trying to display information on the page that the ajax request is in progress, but I cannot figure out how to do it. Tried like this:

var html = $.ajax({ beforeSend: function(){ var id = $(this).attr('href'); var winH = $(window).height(); var winW = $(window).width(); $(id).css('top', winH/2-$(id).height()/2); $(id).css('left', winW/2-$(id).width()/2); $(id).fadeIn(10); }, type: "GET", url: "getcontent.php", data: "json=1&element="+el+"&dependence="+dep, async: false, success: function(){ $('#mask, .window').hide(); } }).responseText; 

What am I doing wrong? But if you put async: true, then the json structure will not have time to fill in! And accordingly the ajax request is rendered useless!

    1 answer 1

    Well, you need to perform an asynchronous request, otherwise why do you need ajax at all?

    You do not need any beforeSend - just make a request and immediately display a message that the request is being executed. Once the request is completed, remove this message.

    About the structure of json, which does not have time to fill, did not understand anything. But if you just make async = true in your code, it will be something completely different. It is necessary to write a normal asynchronous code.

    You need something like this logic:

     $.get("getcontent.php", { "json": "1", "element": el, "dependence": dep }, function(data){ // Проверить, не случилось ли ошибки // Убрать сообщение, что запрос выполняется // Обработать полученные данные (например, записать текст в нужный элемент) }, "json"); // Вывести сообщение «запрос обрабатывается» 

    I highly recommend reading the jQuery.get documentation .