1

In the following code, the controller is not invoking the third $http.get() call and instead goes directly goes to the end.

I want to execute all $http.get() requests. In my code, the third $http.get() depends on the second $http.get() result. Additionally, the second $http.get() depends on the first $http.get() result.

Code

Does anyone have an idea why the third $http.get() is not being invoked?

6
  • 1
    Use your debugger to find out why. Commented Apr 22, 2015 at 5:48
  • because your are making ajax in for loop, without waiting for promise so data.orderedDinnerCarts doesn't have value Commented Apr 22, 2015 at 6:03
  • @ pankajparkar You are right.. So can you pls tell me how to wait for promise... Commented Apr 22, 2015 at 6:28
  • @SANDEEPPANDA Use .then() function instead doing like this.Implement thenable structure here. Commented Apr 22, 2015 at 6:29
  • @Satyam Koyani I put then() instead of success() but still not working.... Commented Apr 22, 2015 at 6:38

2 Answers 2

1

with the way you structured your code, this code below will run before the third $http.get

orderDetails['orderData'] = data;
orderDetails['kitNames'] = kitNames;
orderDetails['proteins'] = proteins;
orderDetails['dietaries'] = dietaries;
orderDetails['ingredients'] = ingredients;

you would have to put it in the third $http.get and every other code that depends on it, and that should solve your challenge

Also, your code can be refactored, so it is more readable and understandable for you to read

Sign up to request clarification or add additional context in comments.

2 Comments

But the controller not comming to the third $http.get()...If I put alert(1) inside third $http.get() its not comming
That means the third $http.get returned error, can you test it out on postman to see if it returned results can you post it on plnkr, and send link
0

Suggestion:-

Actually this is a big issue with angular http call, Because http call does not support async :false

See my answer in this discussion : Angularjs $http VS jquery $.ajax

Your code look like a lot of confusions for call new http method inside of inside methods.

And your code looks like not standard format also gives lot of confusion.

So if you change any code in this file after some day's, then you need to put a microscope glass in your eye, and will see one by one lines. it's will take more times. So avoid http call only for this type of situation.

Solution:-

Avoid http call means, pleas do with Ajax call with async:false option

The code look like

$.ajax({
    type:       "GET",
    dataType:   "Json",
    url:        'Your URL',
    **async:      false,**

    success: function (returndata, status, jqxhr) {
        $(this).html(returndata).hide().fadeIn();
    }).fail(function() { 
        alert("error"); 
    })
});

Explanation About Async:false

Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called. If you set async: true then that statement will begin it's execution and the next statement will be called regardless of whether the async statement has completed yet.

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.