Immediately after sending a request to the server, the ajax function terminates without waiting for a response.

How to make the success method come after receiving a response from the server? As far as I understand it, async: should be true so that the createInProgress method continues to work and the user can see the progress line.

.js

 $(document).ready(function () { var options = { success: function (responce) { //сюда заходит alert("Success"); }, error: function () { $('div#in_progress').remove(); }, }; $("#getSityData").ajaxForm(options); }); function executeOperation() { var url = "/getSityData", city = "Berlin"; createInProgress('progressboxSityData'); $.ajax({ type: 'POST', url: url, data: {city: city}, async: true, dataType: 'json', success: function (response) { //сюда не заходит $('div#in_progress').remove(); } }); } 
  • 2
    and what's actually wrong? that's why he is async, because he immediately gives control, and the result, for example, in success, processes when he receives it - Grundy
  • @Grundy TS apparently interprets timeout as a parameter "how much to do nothing while a request is being made" - Alexey Shimansky
  • Is there really no canonical answer for asynchronous? - D-side
  • one
    @ D-side, either not, or they are some kind of dismal everything - Grundy
  • Try to write more detailed questions. To get an answer, explain exactly what you see the problem, how to reproduce it, what you want to get as a result, etc. Give a sample code. - Grundy

1 answer 1

Perhaps the problem was really in ajaxForm . But removing the dataType: "json", all worked! Although, I think this is a bad decision, but in my case it works :)

  • Most likely the problem was in <input type="submit"> . From what I read, I realized that the action goes to success since for submit constantly performs some actions in the browser. To cancel browser actions, use event.preventDefault (); You can also solve the problem using button instead of submit . - Giovanni