0

I'm building an angular application, there are many ajax calls. I need to wait for each call response to get data from it, that why I make the synchronous.

$.ajax({
    type: "POST",
    url: servicePath,
    cache: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false,
    data: "{}",
    success: success,
    error: error
});

But this is a problem because if I call "showPleaseWait()"(this is a loading popup) it is not showing, because DOM is not updating unless ajax call is not complete. It's showing if I'll make my call async, but I can't get my data that time.

Please, can you help me to solve this problem?

3
  • 2
    Solve your problem by learning how to manage callbacks instead of using (deprecated) synchronous requests. Commented Jul 31, 2015 at 14:27
  • possible duplicate of angular -- accessing data of multiple http calls - how to resolve the promises Commented Jul 31, 2015 at 14:28
  • Call showPleaseWait() before $.ajax, and call hidePleaseWait() in the success or error callback. Async is the power of Ajax, why not? Commented Jul 31, 2015 at 14:59

3 Answers 3

0

If you build a angular app you should take a look at https://docs.angularjs.org/api/ng/service/$http or https://docs.angularjs.org/api/ngResource/service/$resource

If you need to make the ajax calls one after another you can take a look at chaining promises

var a = $http.get('/your-url');
var b = a.then(function(result) {
     // use the results here ...
     return $http.get('/your-other-url');
});
b.then(function(result) {
    // use the results here ...
})
Sign up to request clarification or add additional context in comments.

Comments

0

Its not uncommon for Angular beginners to be thoroughly confused, however you are going wrong in two places. I would recommend some studying. Without encouraging spoon-feeding, here are some good places two start.

  1. You need to read about what's the right way to make ajax data calls in angular. Here's a good place to start :(https://github.com/johnpapa/angular-styleguide#data-services)

  2. Use of JavaScript promises in Angular (https://github.com/johnpapa/angular-styleguide#return-a-promise-from-data-calls) .

All this because you did say you want to use angular.However, lookup how you can use promises with $ajax function in jQuery

Comments

-2

Maybe try to call your showPleaseWait() function, then the $scope.$apply() from angular right before to send the ajax request? It should force angular to refresh the display.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.